Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

C# - Modular Asset Issue

Discussion in 'Scripting' started by iamsickinmymind, Jul 30, 2019.

  1. iamsickinmymind

    iamsickinmymind

    Joined:
    Sep 20, 2018
    Posts:
    2
    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?

    Thanks in advance,
    Dominik

     
  2. Welcome to Unity

    - 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
     
    Suddoha likes this.
  3. iamsickinmymind

    iamsickinmymind

    Joined:
    Sep 20, 2018
    Posts:
    2
    Hi and thanks for a reply!

    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:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Enum_SwordSettings : MonoBehaviour {
    6.  
    7.     // drag'n'drop premade sword component in here
    8.     public GameObject AssetComponent;
    9.  
    10.     public Material[] AssetMaterials;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.        
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.        
    20.     }
    21.  
    22.     // Returns GameObject
    23.     GameObject GetAssetComponent()
    24.     {
    25.         return AssetComponent;
    26.     }
    27. }
    Data Table:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DT_DataTable : MonoBehaviour {
    6.  
    7.     // Array of Settings for the Component
    8.     public Enum_SwordSettings[] SwordSettings;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.        
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.        
    18.     }
    19. }
    Sword Class:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [ExecuteInEditMode]
    6.  
    7. public class Sword : MonoBehaviour {
    8.  
    9.     // Variables of the Modular Sword Class
    10.     /// <summary>
    11.     /// Grip Settings
    12.     /// </summary>
    13.  
    14.     public GameObject thisPrefab;
    15.  
    16.     // Set of Grip Components with defined prefab assets and materials
    17.     public DT_DataTable GripTable;
    18.     public int GripPrefabIndex;
    19.     public int GripMaterialIndex;
    20.     private GameObject GripMesh;
    21.     private Transform GripSocket;
    22.  
    23.     /// <summary>
    24.     /// Crossguard Settins
    25.     /// </summary>
    26.     // Set of Crossguard Components with defined prefab assets and materials
    27.     public DT_DataTable CrossguardTable;
    28.     public int CrossguardPrefabIndex;
    29.     public int CrossguardMaterialIndex;
    30.     private GameObject CrossguardMesh;
    31.     private Transform CrossguardSocket;
    32.  
    33.     /// <summary>
    34.     /// Pommel Settings
    35.     /// </summary>
    36.     // Set of Pommel Components with defined prefab assets and materials
    37.     public DT_DataTable PommelTable;
    38.     public int PommelPrefabIndex;
    39.     public int PommelMaterialIndex;
    40.     private GameObject PommelMesh;
    41.     private Transform PommelSocket;
    42.  
    43.     /// <summary>
    44.     /// Blade Settings
    45.     /// </summary>
    46.     // Set of Blade Components with defined prefab assets and materials
    47.     public DT_DataTable BladeTable;
    48.     public int BladePrefabIndex;
    49.     public int BladeMaterialIndex;
    50.     private GameObject BladeMesh;
    51.     private Transform BladeSocket;
    52.  
    53.     // Use this for initialization
    54.     void Start() {
    55.  
    56.         InitializeSword();
    57.  
    58.     }
    59.  
    60.     // Awake is called once the script is initialized
    61.     void Awake()
    62.     {
    63.  
    64.         InitializeSword();
    65.  
    66.     }
    67.  
    68.     // Update is called once per frame
    69.     void Update() {
    70.  
    71.         InitializeSword();
    72.  
    73.     }
    74.  
    75.     /// Initializes the Sword
    76.     //  This function will try to getGameObject and getMaterial based on DT_DataSettings and Enum_SwordSettings
    77.     void InitializeSword()
    78.     {
    79.  
    80.         /// Initialize Sword Components
    81.         //  Initialize Grip
    82.         GripMesh = GetGameObject(GripTable, GripPrefabIndex);
    83.         if (!GripMesh)
    84.         {
    85.             Debug.Log("Cannot find Grip Mesh. Please, insure that you have correctly setup the 'Grip Settings'.");
    86.             return;
    87.         }
    88.         GripMesh.GetComponent<Renderer>().material = GetMaterial(GripTable, GripPrefabIndex, GripMaterialIndex);
    89.         GripSocket = GetSocketTransform("Grip");
    90.  
    91.         //  Initialize Crossguard
    92.         CrossguardMesh = GetGameObject(CrossguardTable, CrossguardPrefabIndex);
    93.         if (!CrossguardMesh)
    94.         {
    95.             Debug.Log("Cannot find Crossguard Mesh. Please, insure that you have correctly setup the 'Crossguard Settings'.");
    96.             return;
    97.         }
    98.         CrossguardMesh.GetComponent<Renderer>().material = GetMaterial(CrossguardTable, CrossguardPrefabIndex, CrossguardMaterialIndex);
    99.         CrossguardSocket = GetSocketTransform("Crossguard");
    100.  
    101.         //  Initialize Pommel
    102.         PommelMesh = GetGameObject(PommelTable, PommelPrefabIndex);
    103.         if (!PommelMesh)
    104.         {
    105.             Debug.Log("Cannot find Pommel Mesh. Please, insure that you have correctly setup the 'Pommel Settings'.");
    106.             return;
    107.         }
    108.         PommelMesh.GetComponent<Renderer>().material = GetMaterial(PommelTable, PommelPrefabIndex, PommelMaterialIndex);
    109.         PommelSocket = GetSocketTransform("Pommel");
    110.  
    111.         //  Initialize Nlade
    112.         BladeMesh = GetGameObject(BladeTable, BladePrefabIndex);
    113.         if (!BladeMesh)
    114.         {
    115.             Debug.Log("Cannot find Blade Mesh. Please, insure that you have correctly setup the 'Blade Settings'.");
    116.             return;
    117.         }
    118.         BladeMesh.GetComponent<Renderer>().material = GetMaterial(BladeTable, BladePrefabIndex, BladeMaterialIndex);
    119.         BladeSocket = GetSocketTransform("Blade");
    120.  
    121.         SetMesh();
    122.                
    123.     }
    124.  
    125.     /// Gets the GameObject from DataTable
    126.     //  Get GameObject from Data Table at given Index
    127.     //  If cannot find GameObject at given Index, try to get GameObject from 0th position
    128.     //  Else return
    129.     GameObject GetGameObject(DT_DataTable DataTable, int GameObjectIndex)
    130.     {
    131.         if (DataTable.SwordSettings[GameObjectIndex].AssetComponent.gameObject)
    132.         {
    133.             return DataTable.SwordSettings[GameObjectIndex].AssetComponent.gameObject;
    134.         }
    135.         else
    136.         {
    137.             if (DataTable.SwordSettings[0].AssetComponent.gameObject)
    138.             {
    139.                 return DataTable.SwordSettings[0].AssetComponent.gameObject;
    140.             }
    141.             else
    142.             {
    143.                 Debug.Log("Cannot find a GameObject from Data Table at given Index. Please, insure that you have correctly setup the Data Tables.");
    144.                 return null;
    145.             }
    146.         }
    147.     }
    148.  
    149.     /// Get Material from DataTable
    150.     //  Get Material from Data Table at given Index
    151.     //  If cannot find Material at given Index, try to get Material from 0th position
    152.     //  If cannot find Material for Asset at given Index, try to get 0th Asset and its 0th Material
    153.     //  Else return
    154.     Material GetMaterial(DT_DataTable DataTable, int GameObjectIndex, int MaterialIndex)
    155.     {
    156.         if (DataTable.SwordSettings[GameObjectIndex].AssetMaterials[MaterialIndex])
    157.         {
    158.             return DataTable.SwordSettings[GameObjectIndex].AssetMaterials[MaterialIndex];
    159.         }
    160.         else
    161.         {
    162.             if (DataTable.SwordSettings[GameObjectIndex].AssetMaterials[0])
    163.             {
    164.                 return DataTable.SwordSettings[GameObjectIndex].AssetMaterials[0];
    165.             }
    166.             else
    167.             {
    168.                 if (DataTable.SwordSettings[0].AssetMaterials[0])
    169.                 {
    170.                     return DataTable.SwordSettings[0].AssetMaterials[0];
    171.                 }
    172.                 else
    173.                 {
    174.                     Debug.Log("Cannot find a Material from Data Table at given Index. Please, insure that you have correctly setup the Data Tables.");
    175.                     return null;
    176.                 }
    177.             }
    178.         }
    179.     }
    180.  
    181.     Transform GetSocketTransform(string SocketName)
    182.     {
    183.         if(SocketName == "Grip")
    184.             return thisPrefab.transform.Find("Grip");
    185.  
    186.         if(SocketName == "Crossguard")
    187.             return thisPrefab.transform.Find("Grip").transform.Find("Crossguard Socket");
    188.  
    189.         if(SocketName == "Blade")
    190.             return thisPrefab.transform.Find("Grip").transform.Find("Crossguard Socket").transform.Find("Crossguard").transform.Find("Blade Socket");
    191.  
    192.         if (SocketName == "Pommel")
    193.             return thisPrefab.transform.Find("Grip").transform.Find("Pommel Socket");
    194.  
    195.         else
    196.             return null;
    197.     }
    198.  
    199.     void SetMesh()
    200.     {
    201.         thisPrefab.transform.Find("Grip").GetComponent<MeshFilter>().sharedMesh = GripMesh.GetComponent<MeshFilter>().sharedMesh;
    202.         thisPrefab.transform.Find("Grip").transform.Find("Crossguard Socket").transform.Find("Crossguard").GetComponent<MeshFilter>().sharedMesh = CrossguardMesh.GetComponent<MeshFilter>().sharedMesh;
    203.         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;
    204.         thisPrefab.transform.Find("Grip").transform.Find("Pommel Socket").transform.Find("Pommel").GetComponent<MeshFilter>().sharedMesh = PommelMesh.GetComponent<MeshFilter>().sharedMesh;
    205.     }
    206. }