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

Is there a way to make a component serializable?

Discussion in 'Scripting' started by Draconic, Jul 2, 2014.

  1. Draconic

    Draconic

    Joined:
    Oct 12, 2013
    Posts:
    82
    I am making a custom text editor that can be changed in the inspector. You can change all properties of GUIText, except I had to create a class that has the same properties as GUIText and make that serializable so I can edit it in the inspector. I would rather just have a serialized public GUIText, that you can edit in the inspector.

    I have found something called SerializeField, but I don't really understand it, so could I use this to get the effect I want?
     
  2. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Edit: didnt read your text properly :D
     
    Last edited: Jul 2, 2014
  3. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    I'm not sure what you mean... Component are always serializable, with the GameObject that own them.

    Do you mean serializable OUTSIDE of a GameObject/Prefab?
     
  4. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    you use [SerializeField] above a private field variable if you serialize your class with [System.Serializable]

    Code (CSharp):
    1. [System.Serializable]
    2. public class Example
    3. {
    4.           [SerializeField]
    5.           private int _importantNumber;
    6.           //public fields get serialize automaticly
    7.           public string name;
    8.           public Example(string s)
    9.           {
    10.                     name = s;
    11.           }
    12.           public int ImportantNumber
    13.           {
    14.                  get{return _importantNumber;}
    15.                  set{_importantNumber = value;}
    16.            }
    17. }
     
  5. Draconic

    Draconic

    Joined:
    Oct 12, 2013
    Posts:
    82
    I don't really know how it works... I thought it just meant that it could be edited in the inspector. Is there any way that I can have an editable component such as GUIText as a variable in a script?

    So, if I made a:
    public GUIText text = new GUIText();

    Is there any way that I can make that particular new GUIText editable from that variable in the inspector?
     
  6. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    So you want to write a string in the inspector which is set to the GUI text's text?

    Just save a string and then set GUiText.text to the string.
     
  7. Draconic

    Draconic

    Joined:
    Oct 12, 2013
    Posts:
    82
    I do want to be able to edit the string from the inspector, but is there any way to edit everything about a new public GUIText (text, color, pixeloffset etc) from the inspector without creating a new class which contains everything about GUIText, then sets the values to that?
     
  8. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    It's a bit the limitation of component, you cannot do "new MyComponent", they must be created with "gameObject.AddComponent".

    Now, why you don't want your GameObject to have a GUIText on it, so you can edit it in the inspector?