Search Unity

Question Which one of these methods of customizing an object is the way to go?

Discussion in 'Editor & General Support' started by stakensj, Mar 26, 2023.

  1. stakensj

    stakensj

    Joined:
    Oct 23, 2020
    Posts:
    35
    So I came up with these two ways, one using scriptable objects, and the other one disables and enables game objects.

    Method 1:
    This method uses scriptable objects, I simply assign a lot of different prefabs for different objects, which I then instantiate on the designated transform of the main GameObjects, however, the code for this is very annoying to write and hard to read
    Scriptable Object:

    The Game Object and its children:

    the attachment controller:

    The scriptable object code:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/ScriptableWeapons", order = 1)]
    4. public class ScriptableWeapons : ScriptableObject
    5. {
    6.     public GameObject[] BarPrefabs;
    7.     public GameObject[] HandPrefabs;
    8.     public GameObject[] HandlePrefabs;
    9.     public GameObject[] MagPrefabs;
    10.     public GameObject[] MuzzlePrefabs;
    11.     public GameObject[] ScoPrefabs;
    12.     public GameObject[] SideLPrefabs;
    13.     public GameObject[] SideRPrefabs;
    14.     public GameObject[] StockPrefabs;
    15.     public GameObject[] TriPrefabs;
    16.     public GameObject[] UbderbPrefabs;
    17.    
    18. }
    19.  
    The Manager Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AttachementController : MonoBehaviour
    6. {
    7.    public bool GunLevelOn = true;
    8.  
    9.    public int GunLevel = 1;
    10.    public int  Mag = 1;
    11.    public int  Trigger = 1;
    12.    public int  Scope = 1;
    13.    public int  Barrel = 1;
    14.    public int  Ubderbarrel = 1;
    15.    public int  Muzzle = 1;
    16.    public int  Stock = 1;
    17.    public int  HandGuard = 1;
    18.    public int  SideL = 1;
    19.    public int  SideR = 1;
    20.    public int  Handle = 1;
    21.  
    22.    bool CanUnder = true;
    23.  
    24.    public Transform MagPosition;
    25.    public Transform ScoPosition;
    26.    public Transform StockPosition;
    27.    public Transform BarPosition;
    28.    public Transform TriPosition;
    29.    public Transform UbderbPosition;
    30.    public Transform MuzzlePosition;
    31.    public Transform HandPosition;
    32.    public Transform SideLPosition;
    33.    public Transform SideRPosition;
    34.    public Transform HandlePosition;
    35.  
    36.   // public Transform RealMuzzle;
    37.  
    38. /*
    39.      Mag
    40.      Tri
    41.      Sto
    42.      Sco
    43.      Bar
    44.      Ubderb
    45.      Muzzle
    46.      Hand
    47.      SideL
    48.      SideR
    49.      Handle
    50.    */
    51.  
    52.     GameObject currentMagz;
    53.     GameObject currentScopez;
    54.     GameObject currentStockz;
    55.     GameObject currentBarrelz;
    56.     GameObject currentTriggerz;
    57.     GameObject currentUbderbz;
    58.     GameObject currentMuzzlez;
    59.     GameObject currentHandz;
    60.     GameObject currentSideLz;
    61.     GameObject currentSideRz;
    62.     GameObject currentHandlez;
    63.  
    64.     int currentmag;
    65.     int currentscope;
    66.     int currentstock;
    67.     int currentbarrel = 0;
    68.     int currentTri;
    69.     int currentBar;
    70.     int currentUbderb;
    71.     int currentMuzzle = 1;
    72.     int currentHand;
    73.     int currentSideL;
    74.     int currentSideR;
    75.     int currentHandle;
    76.  
    77.     public ScriptableWeapons AttachementValues;
    78.  
    79.     // This will be appended to the name of the created entities and increment when each is created.
    80.    // int instanceNumber = 1;
    81.  
    82.     void FixedUpdate()
    83.     {   if (GunLevelOn){
    84.          Mag = GunLevel;
    85.          Scope= GunLevel;
    86.          Stock = GunLevel;
    87.          Barrel = GunLevel;
    88.         }
    89.  
    90.        // currentUbderbz.SetActive(CanUnder);
    91.  
    92.         Barrel = Mathf.Clamp(Barrel, 1, AttachementValues.BarPrefabs.Length);
    93.         HandGuard = Mathf.Clamp(HandGuard, 1, AttachementValues.HandPrefabs.Length);
    94.         Handle = Mathf.Clamp(Handle, 1, AttachementValues.HandlePrefabs.Length);
    95.         Mag = Mathf.Clamp(Mag, 1, AttachementValues.MagPrefabs.Length);
    96.         Muzzle = Mathf.Clamp(Muzzle, 1, AttachementValues.MuzzlePrefabs.Length);
    97.         Scope = Mathf.Clamp(Scope, 1, AttachementValues.ScoPrefabs.Length);
    98.         SideL = Mathf.Clamp(SideL, 1, AttachementValues.SideLPrefabs.Length);
    99.         SideR = Mathf.Clamp(SideR, 1, AttachementValues.SideRPrefabs.Length);
    100.         Stock = Mathf.Clamp(Stock, 1, AttachementValues.StockPrefabs.Length);
    101.         Trigger = Mathf.Clamp(Trigger, 1, AttachementValues.TriPrefabs.Length);
    102.         Ubderbarrel = Mathf.Clamp(Ubderbarrel, 1, AttachementValues.UbderbPrefabs.Length);
    103.        // Debug.Log(AttachementValues.MagPrefabs.Length);
    104.        // MuzzlePosition.transform.position = currentBarrelz.transform.GetChild(0).transform.position;
    105.  
    106.  
    107.        // Ubderbarrel= (Ubderbarrel + 1) % AttachementValues.UbderbPrefabs.Length;
    108.        
    109.         if (currentmag != Mag)
    110.         {
    111.             Destroy(currentMagz);
    112.             currentMagz = Instantiate(AttachementValues.MagPrefabs[Mag-1], MagPosition.transform.position , MagPosition.transform.rotation, this.transform);
    113.             currentMagz.transform.parent = gameObject.transform;
    114.             currentMagz.transform.position = MagPosition.transform.position;
    115.             currentMagz.name = "currentmagz";
    116.             currentmag = Mag;
    117.         }
    118.         if (currentscope != Scope)
    119.         {
    120.             Destroy(currentScopez);
    121.             currentScopez = Instantiate(AttachementValues.ScoPrefabs[Scope-1], ScoPosition.transform.position , ScoPosition.transform.rotation, this.transform);
    122.             currentScopez.transform.parent = gameObject.transform;
    123.             currentScopez.transform.position = ScoPosition.transform.position;
    124.             currentScopez.name = "currentscopez";
    125.             currentscope = Scope;
    126.         }
    127.         if (currentstock != Stock)
    128.         {
    129.             Destroy(currentStockz);
    130.             currentStockz = Instantiate(AttachementValues.StockPrefabs[Stock-1], StockPosition.transform.position , StockPosition.transform.rotation, this.transform);
    131.             currentStockz.transform.parent = gameObject.transform;
    132.             currentStockz.transform.position = StockPosition.transform.position;
    133.             currentStockz.name = "currentstockz";
    134.             currentstock = Stock;
    135.         }
    136.         if (currentbarrel != Barrel)
    137.         {
    138.             Destroy(currentBarrelz);
    139.             currentBarrelz = Instantiate(AttachementValues.BarPrefabs[Barrel-1], BarPosition.transform.position , BarPosition.transform.rotation, this.transform);
    140.             currentBarrelz.transform.parent = gameObject.transform;
    141.             currentBarrelz.transform.position = BarPosition.transform.position;
    142.             currentBarrelz.name = "currentbarrelz";
    143.             currentbarrel = Barrel;
    144.             MuzzlePosition.position = currentBarrelz.transform.GetChild(0).transform.position;
    145.             currentMuzzlez.transform.position = MuzzlePosition.position;
    146.         }
    147.         if (currentMuzzle != Muzzle)
    148.         {
    149.             Destroy(currentMuzzlez);
    150.             currentMuzzlez = Instantiate(AttachementValues.MuzzlePrefabs[Muzzle-1], MuzzlePosition.transform.position , MuzzlePosition.transform.rotation, this.transform);
    151.             currentMuzzlez.transform.parent = gameObject.transform;
    152.             currentMuzzlez.transform.position = MuzzlePosition.transform.position;
    153.             currentMuzzlez.name = "currentmuzzlez";
    154.             currentMuzzle = Muzzle;
    155.         }
    156.         if (currentHand != HandGuard)
    157.         {
    158.             if(HandGuard >= 5) {
    159.                 CanUnder = false;
    160.             }
    161.             else{
    162.                 CanUnder = true;
    163.             }
    164.             Destroy(currentHandz);
    165.             currentHandz = Instantiate(AttachementValues.HandPrefabs[HandGuard-1], HandPosition.transform.position , HandPosition.transform.rotation, this.transform);
    166.             currentHandz.transform.parent = gameObject.transform;
    167.             currentHandz.transform.position = HandPosition.transform.position;
    168.             currentHandz.name = "currentHandz";
    169.             currentHand = HandGuard;
    170.             if(CanUnder){
    171.             UbderbPosition.position = currentHandz.transform.GetChild(0).transform.position;
    172.             currentUbderbz.transform.position = UbderbPosition.position;
    173.             SideRPosition.position = currentHandz.transform.GetChild(1).transform.position;
    174.             currentSideRz.transform.position = SideRPosition.position;
    175.             SideRPosition.rotation = currentHandz.transform.GetChild(1).transform.rotation;
    176.             currentSideRz.transform.rotation = SideRPosition.rotation;
    177.             SideLPosition.position = currentHandz.transform.GetChild(2).transform.position;
    178.             currentSideLz.transform.position = SideLPosition.position;
    179.             SideLPosition.rotation = currentHandz.transform.GetChild(2).transform.rotation;
    180.             currentSideLz.transform.rotation = SideLPosition.rotation;
    181.             }
    182.            
    183.         }
    184.         if (currentHandle != Handle)
    185.         {
    186.             Destroy(currentHandlez);
    187.             currentHandlez = Instantiate(AttachementValues.HandlePrefabs[Handle-1], HandlePosition.transform.position , HandlePosition.transform.rotation, this.transform);
    188.             currentHandlez.transform.parent = gameObject.transform;
    189.             currentHandlez.transform.position = HandlePosition.transform.position;
    190.             currentHandlez.name = "currentHandlez";
    191.             currentHandle = Handle;
    192.         }
    193.         if (currentSideL != SideL)
    194.         {
    195.             Destroy(currentSideLz);
    196.             currentSideLz = Instantiate(AttachementValues.SideLPrefabs[SideL-1], SideLPosition.transform.position , SideLPosition.transform.rotation, this.transform);
    197.             currentSideLz.transform.parent = gameObject.transform;
    198.             currentSideLz.transform.position = SideLPosition.transform.position;
    199.             currentSideLz.name = "currentSideL";
    200.             currentSideL = SideL;
    201.         }
    202.         if (currentSideR != SideR)
    203.         {
    204.             Destroy(currentSideRz);
    205.             currentSideRz = Instantiate(AttachementValues.SideRPrefabs[SideR-1], SideRPosition.transform.position , SideRPosition.transform.rotation, this.transform);
    206.             currentSideRz.transform.parent = gameObject.transform;
    207.             currentSideRz.transform.position = SideRPosition.transform.position;
    208.             currentSideRz.name = "currentSideR";
    209.             currentSideR = SideR;
    210.         }
    211.         if (currentTri != Trigger)
    212.         {
    213.             Destroy(currentTriggerz);
    214.             currentTriggerz = Instantiate(AttachementValues.TriPrefabs[Trigger-1], TriPosition.transform.position , TriPosition.transform.rotation, this.transform);
    215.             currentTriggerz.transform.parent = gameObject.transform;
    216.             currentTriggerz.transform.position = TriPosition.transform.position;
    217.             currentTriggerz.name = "currentTrigger";
    218.             currentTri = Trigger;
    219.         }
    220.         if (currentUbderb != Ubderbarrel)
    221.         {
    222.             Destroy(currentUbderbz);
    223.             currentUbderbz = Instantiate(AttachementValues.UbderbPrefabs[Ubderbarrel-1], UbderbPosition.transform.position , UbderbPosition.transform.rotation, this.transform);
    224.             currentUbderbz.transform.parent = gameObject.transform;
    225.             currentUbderbz.transform.position = UbderbPosition.transform.position;
    226.             currentUbderbz.name = "currentUnderb";
    227.             currentUbderb=  Ubderbarrel;
    228.                      
    229.         }
    230.         currentUbderbz.SetActive(CanUnder);
    231.         currentSideRz.SetActive(CanUnder);
    232.         currentSideLz.SetActive(CanUnder);
    233.     }
    234.  
    235. }
    236.  
    in the second method, I simply add all the possible to the game object and enable disable them on demand:

    I have not written code for this one however, since I want to firstly know is there an option of improving the first method.
     
  2. stakensj

    stakensj

    Joined:
    Oct 23, 2020
    Posts:
    35
    update: I decided to semi-optimize the first method to separate the single scriptable objects into multiple,
    I basically made it so you assign game objects of the first category(barrel) into the Barrel dedicated Scriptable Object, and then assign those into the main Scriptable Object