Search Unity

How can I use { get; } to assign a new guid ?

Discussion in 'Scripting' started by SharonL75, Oct 11, 2020.

  1. SharonL75

    SharonL75

    Joined:
    Aug 13, 2020
    Posts:
    91
    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class GenerateState : MonoBehaviour, IStateQuery
    8. {
    9.     private State m_state = new State();
    10.     private Guid guidID;
    11.  
    12.     private void Awake()
    13.     {
    14.         guidID = Guid.NewGuid();
    15.     }
    16.  
    17.     public Guid UniqueId => Guid.Parse(guidID.ToString());
    18.  
    19.     private class State
    20.     {
    21.         // Properties
    22.     }
    23.  
    24.     public string GetState()
    25.     {
    26.         return JsonUtility.ToJson(m_state);
    27.     }
    28.  
    29.     public void SetState(string jsonString)
    30.     {
    31.         m_state = JsonUtility.FromJson<State>(jsonString);
    32.     }
    33. }
    34.  
    I'm using the UniqueId in other scripts. I want to generate automatic new guid for it.
    Before that I used it like this :

    Code (csharp):
    1.  
    2. private State m_state = new State();
    3.  
    4.     public Guid UniqueId => Guid.Parse("571E6219-DFD4-4893-A3B0-F9A151BFABFE");
    5.  
    6.     private class State
    7.     {
    8.         public bool s1;
    9.     }
    10.  
    11.     public string GetState()
    12.     {
    13.         return JsonUtility.ToJson(m_state);
    14.     }
    15.  
    16.     public void SetState(string jsonString)
    17.     {
    18.         m_state = JsonUtility.FromJson<State>(jsonString);
    19.     }
    20.  
    But instead generating in the visual studio a new guid number copy and paste it over and over again because I'm using this script in multiple objects I want to assign automatic guid to it.

    This add two objects to each selected gameobject including the GenerateState script :

    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEditor;
    6. using UnityEngine;
    7.  
    8. public class GenerateAutomaticGuid : Editor
    9. {
    10.     [MenuItem("GameObject/Generate Guid", false, 11)]
    11.     private static void GenerateGuid()
    12.     {
    13.         foreach (GameObject o in Selection.gameObjects)
    14.         {
    15.             var h = o.GetComponent<GenerateState>();
    16.             {
    17.                 if (!h) o.AddComponent<GenerateState>();
    18.             }
    19.  
    20.             var g = o.GetComponent<GenerateGuid>();
    21.             if (!g)
    22.             {
    23.                 g = o.AddComponent<GenerateGuid>();
    24.                 g.GenerateGuidNum();
    25.             }
    26.         }
    27.     }
    28. }
    29.  
    That is why I want to generate automatic guid in the GenerateState script in this line :
    The way it is now it's not getting to this line :

    Code (csharp):
    1.  
    2. public Guid UniqueId => Guid.Parse(guidID.ToString());
    3.  
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    I'm not sure if I understand. Are you saying you want the GUID to be generated automatically in the editor instead of during runtime?

    If so, you could use the MonoBehaviour.Reset callback method to do so.
    This method is called whenever a script first loads and when you select the "Reset" option from a MonoBehaviour's context menu.

    Something like this should work:
    Code (CSharp):
    1. public class GenerateState : MonoBehaviour {
    2.    private Guid guidID;
    3.  
    4. #if UNITY_EDITOR
    5.    private bool editorGuidGenerated = false;
    6.  
    7.    private void Reset() {
    8.       if(!editorGuidGenerated) {
    9.          guidID = new Guid();
    10.          editorGuidGenerated = true;
    11.       }
    12.    }
    13. #endif
    14. }
     
    JanH likes this.