Hi,
My name is Dominik and I’m Unreal Engine 4 developer. So what am I doing here? I’m trying to switch some of my projects from UE4 to Unity (would you believe that?).
However, I’m facing an issue I don’t have enough experience to solve by myself. My most recent project is a Modular Sword where the modularity is semi-automated (as you can see in the video from UE4).
For Unity, I have created some C# scripts which should simulate the behaviour (I can send you the scripts and preview assets in a demo project), however none of those work.
Is there anyone willing me to help?
if you’re looking for people who can develop scripts (or other) for you for money or for free then you should check Unity Connect
if you want to do it for yourself but you need some help because you stuck somewhere then post the code you already did and describe what have you tried and why and what is the problem with it, we’re here to help if you need, but we don’t write entire scripts for any projects
I’m not looking for somebody who could develop a code for me, thank you.
I’m looking for someone who could eventually guide me and tell me how to proceed in a case that I’m facing.
What do I want to achieve:
Each time I change a variable in the Inspector, the code should run and:
Find if there is any existing Data Table
If so, get prefab from Data Table at its position from Sword Settings
If so, get Material from Data Table at its position from Sword Setting’s Array of Materials
Set the Mesh (and its Material) to variables
Set the Mesh and its Material to the Prefab’s child parts accordingly (GripMesh to Grip etc.)
This should happen every time I change any value, including the Transform values like position or scale.
My actual code is here:
Sword Settings:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enum_SwordSettings : MonoBehaviour {
// drag'n'drop premade sword component in here
public GameObject AssetComponent;
public Material[] AssetMaterials;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
// Returns GameObject
GameObject GetAssetComponent()
{
return AssetComponent;
}
}
Data Table:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DT_DataTable : MonoBehaviour {
// Array of Settings for the Component
public Enum_SwordSettings[] SwordSettings;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Sword Class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class Sword : MonoBehaviour {
// Variables of the Modular Sword Class
/// <summary>
/// Grip Settings
/// </summary>
public GameObject thisPrefab;
// Set of Grip Components with defined prefab assets and materials
public DT_DataTable GripTable;
public int GripPrefabIndex;
public int GripMaterialIndex;
private GameObject GripMesh;
private Transform GripSocket;
/// <summary>
/// Crossguard Settins
/// </summary>
// Set of Crossguard Components with defined prefab assets and materials
public DT_DataTable CrossguardTable;
public int CrossguardPrefabIndex;
public int CrossguardMaterialIndex;
private GameObject CrossguardMesh;
private Transform CrossguardSocket;
/// <summary>
/// Pommel Settings
/// </summary>
// Set of Pommel Components with defined prefab assets and materials
public DT_DataTable PommelTable;
public int PommelPrefabIndex;
public int PommelMaterialIndex;
private GameObject PommelMesh;
private Transform PommelSocket;
/// <summary>
/// Blade Settings
/// </summary>
// Set of Blade Components with defined prefab assets and materials
public DT_DataTable BladeTable;
public int BladePrefabIndex;
public int BladeMaterialIndex;
private GameObject BladeMesh;
private Transform BladeSocket;
// Use this for initialization
void Start() {
InitializeSword();
}
// Awake is called once the script is initialized
void Awake()
{
InitializeSword();
}
// Update is called once per frame
void Update() {
InitializeSword();
}
/// Initializes the Sword
// This function will try to getGameObject and getMaterial based on DT_DataSettings and Enum_SwordSettings
void InitializeSword()
{
/// Initialize Sword Components
// Initialize Grip
GripMesh = GetGameObject(GripTable, GripPrefabIndex);
if (!GripMesh)
{
Debug.Log("Cannot find Grip Mesh. Please, insure that you have correctly setup the 'Grip Settings'.");
return;
}
GripMesh.GetComponent<Renderer>().material = GetMaterial(GripTable, GripPrefabIndex, GripMaterialIndex);
GripSocket = GetSocketTransform("Grip");
// Initialize Crossguard
CrossguardMesh = GetGameObject(CrossguardTable, CrossguardPrefabIndex);
if (!CrossguardMesh)
{
Debug.Log("Cannot find Crossguard Mesh. Please, insure that you have correctly setup the 'Crossguard Settings'.");
return;
}
CrossguardMesh.GetComponent<Renderer>().material = GetMaterial(CrossguardTable, CrossguardPrefabIndex, CrossguardMaterialIndex);
CrossguardSocket = GetSocketTransform("Crossguard");
// Initialize Pommel
PommelMesh = GetGameObject(PommelTable, PommelPrefabIndex);
if (!PommelMesh)
{
Debug.Log("Cannot find Pommel Mesh. Please, insure that you have correctly setup the 'Pommel Settings'.");
return;
}
PommelMesh.GetComponent<Renderer>().material = GetMaterial(PommelTable, PommelPrefabIndex, PommelMaterialIndex);
PommelSocket = GetSocketTransform("Pommel");
// Initialize Nlade
BladeMesh = GetGameObject(BladeTable, BladePrefabIndex);
if (!BladeMesh)
{
Debug.Log("Cannot find Blade Mesh. Please, insure that you have correctly setup the 'Blade Settings'.");
return;
}
BladeMesh.GetComponent<Renderer>().material = GetMaterial(BladeTable, BladePrefabIndex, BladeMaterialIndex);
BladeSocket = GetSocketTransform("Blade");
SetMesh();
}
/// Gets the GameObject from DataTable
// Get GameObject from Data Table at given Index
// If cannot find GameObject at given Index, try to get GameObject from 0th position
// Else return
GameObject GetGameObject(DT_DataTable DataTable, int GameObjectIndex)
{
if (DataTable.SwordSettings[GameObjectIndex].AssetComponent.gameObject)
{
return DataTable.SwordSettings[GameObjectIndex].AssetComponent.gameObject;
}
else
{
if (DataTable.SwordSettings[0].AssetComponent.gameObject)
{
return DataTable.SwordSettings[0].AssetComponent.gameObject;
}
else
{
Debug.Log("Cannot find a GameObject from Data Table at given Index. Please, insure that you have correctly setup the Data Tables.");
return null;
}
}
}
/// Get Material from DataTable
// Get Material from Data Table at given Index
// If cannot find Material at given Index, try to get Material from 0th position
// If cannot find Material for Asset at given Index, try to get 0th Asset and its 0th Material
// Else return
Material GetMaterial(DT_DataTable DataTable, int GameObjectIndex, int MaterialIndex)
{
if (DataTable.SwordSettings[GameObjectIndex].AssetMaterials[MaterialIndex])
{
return DataTable.SwordSettings[GameObjectIndex].AssetMaterials[MaterialIndex];
}
else
{
if (DataTable.SwordSettings[GameObjectIndex].AssetMaterials[0])
{
return DataTable.SwordSettings[GameObjectIndex].AssetMaterials[0];
}
else
{
if (DataTable.SwordSettings[0].AssetMaterials[0])
{
return DataTable.SwordSettings[0].AssetMaterials[0];
}
else
{
Debug.Log("Cannot find a Material from Data Table at given Index. Please, insure that you have correctly setup the Data Tables.");
return null;
}
}
}
}
Transform GetSocketTransform(string SocketName)
{
if(SocketName == "Grip")
return thisPrefab.transform.Find("Grip");
if(SocketName == "Crossguard")
return thisPrefab.transform.Find("Grip").transform.Find("Crossguard Socket");
if(SocketName == "Blade")
return thisPrefab.transform.Find("Grip").transform.Find("Crossguard Socket").transform.Find("Crossguard").transform.Find("Blade Socket");
if (SocketName == "Pommel")
return thisPrefab.transform.Find("Grip").transform.Find("Pommel Socket");
else
return null;
}
void SetMesh()
{
thisPrefab.transform.Find("Grip").GetComponent<MeshFilter>().sharedMesh = GripMesh.GetComponent<MeshFilter>().sharedMesh;
thisPrefab.transform.Find("Grip").transform.Find("Crossguard Socket").transform.Find("Crossguard").GetComponent<MeshFilter>().sharedMesh = CrossguardMesh.GetComponent<MeshFilter>().sharedMesh;
thisPrefab.transform.Find("Grip").transform.Find("Crossguard Socket").transform.Find("Crossguard").transform.Find("Blade Socket").transform.Find("Blade").GetComponent<MeshFilter>().sharedMesh = BladeMesh.GetComponent<MeshFilter>().sharedMesh;
thisPrefab.transform.Find("Grip").transform.Find("Pommel Socket").transform.Find("Pommel").GetComponent<MeshFilter>().sharedMesh = PommelMesh.GetComponent<MeshFilter>().sharedMesh;
}
}