Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Asset Selection shows Assets with the wrong Type

Discussion in 'Scripting' started by ThomasSoto, May 21, 2023.

  1. ThomasSoto

    ThomasSoto

    Joined:
    Aug 7, 2013
    Posts:
    26
    I'm trying to create a generic class called Value from which I can get a local class or the same type of class from a ScriptableObject.

    Here is a snippet of the code

    Code (CSharp):
    1. using NaughtyAttributes;
    2. using UnityEngine;
    3.  
    4. [System.Serializable]
    5. public class Value <T> {
    6.     [SerializeField] bool isLocal = true;
    7.  
    8.     [SerializeField, AllowNesting, ShowIf (nameof (isLocal))] T value;
    9.     [SerializeField, AllowNesting, HideIf(nameof(isLocal))] ValueSO<T> scriptableObject;
    10.  
    11.     // Getter
    12.     public T Get() {
    13.         if (isLocal) {
    14.             return value;
    15.         } else {
    16.             return scriptableObject.value;
    17.         }
    18.     }
    19. }
    20.  
    21. using UnityEngine;
    22.  
    23. public class ValueSO<T> : ScriptableObject {
    24.     [SerializeField] public T value;
    25. }
    These are 2 examples of Scriptable Objects that would use the Generic class

    Code (CSharp):
    1. using UnityEngine;
    2. public class RecoilStatsSO : ValueSO<RecoilStats> {}
    3.  
    4. using UnityEngine;
    5. public class ModeStatsSO : ValueSO<ModeStats> {}
    Everything works as expected until here. The issue I'm having is that when using the fields as non-local (so they use a scriptableObject) the Unity Asset selection UI shows all ScriptableObjects that use the Generic ValueSO class instead of showing only the ones with the specified <T>

    Code (CSharp):
    1. using UnityEngine;
    2. public class Test : Monobehaviour {
    3.     public class Value <RecoilStats> recoil;
    4.     public class Value <ModeStats> mode;
    5. }



    Does anyone know why this works this way? Or if there is a simpler solution for what I'm doing?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Pretty sure Unity's serialization does not handle generics at all.

    Have you tried just using your concretes that you made, namely
    RecoilStatsSO
    and
    ModeStatsSO
    ?

    I'm pretty sure that might work as you expect...
     
  3. ThomasSoto

    ThomasSoto

    Joined:
    Aug 7, 2013
    Posts:
    26
    Yeah those work perfectly fine but I want the flexibility to use a local variable or the Scriptable Object. Take a look

    With SO example
    upload_2023-5-21_12-47-10.png

    With Local variable example
    upload_2023-5-21_12-47-36.png
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,881
    Unity's normal serialisation has supported generics for a while (and you know this, you've commented on threads about this revelation!).

    However Unity's default object picker has no understanding of generic and will either show the wrong assets or not show any assets at all.

    You will need custom inspectors to aid in picking the correct asset types.
     
    Kurt-Dekker likes this.
  5. ThomasSoto

    ThomasSoto

    Joined:
    Aug 7, 2013
    Posts:
    26
    Thank you for your response! I'll look into that! And I don't recall commenting on a thread about it, but I might have forgotten lol. Thank you again!
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,881
    The first part of my reply was in response to Kurt, not you.
     
  7. ThomasSoto

    ThomasSoto

    Joined:
    Aug 7, 2013
    Posts:
    26