简体   繁体   中英

What is the difference between creating a new guid instance or selecting in Visual Studio tools > create guid?

In my state script I select Tools > Create GUID > New GUID > Copy

新的 GUID

The script is looking like this now :

The guide is not the same as in the screenshot it's just for example.

The problem is that on each object I want to add to this state script format I need manually to generate a new GUID copy it and paste it to the code and it's a bit annoying. So I wonder what is the difference between creating a new guid instance or selecting in visual studio tools > create guide.

This is when creating a new instance :

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GenerateGuid : MonoBehaviour
{
    public string uniqueGuidID;

    private Guid guidID;
   
    public void GenerateGuidNum()
    {
        guidID = Guid.NewGuid();
        uniqueGuidID = guidID.ToString();
    }
}

I wonder why not use the same new instance technic in both cases, why or what the tool for creating guide number 5 is for ?

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StateTest : MonoBehaviour, IStateQuery
{
    private State m_state = new State();

    public Guid UniqueId => Guid.Parse("571E6219-DFD4-4893-A3B0-F9A151BFABFE");


    private class State
    {
        public bool s1;
        // bool the kid holding the navi is true
    }

    public string GetState()
    {
        return JsonUtility.ToJson(m_state);
    }

    public void SetState(string jsonString)
    {
        m_state = JsonUtility.FromJson<State>(jsonString);
    }



    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

I will edit and update the usage of it :

With this script :

using System;

public interface IStateQuery
{
    string GetState();
    void SetState(string jsonString);

    Guid UniqueId { get; }
}

In the TestState script I'm using the IStateQuery :

public class StateTest : MonoBehaviour, IStateQuery

Then use it like this in the Save and Load methods :

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class SaveLoad : MonoBehaviour
{
    public FadeInOutSaveGameText fadeInOutSaveGame;
    public float timeToStartSaving;
    public float savingFadeInOutTime;

    private string saveString;

    private void Awake()
    {
        SaveSystem.Init();
    }

    public void Save(string Folder, string FileName)
    {
        var objectsToSave = UpdateObjectsToSave();

        SaveGame saveGame = new SaveGame();
        saveGame.saveObjects = new List<SaveObject>();
        for (int i = 0; i < objectsToSave.Count; i++)
        {
            SaveObject saveObject = new SaveObject();
            saveObject.transformSaver = new TransformSaver();
            
            Debug.Log($"{i}");
            Debug.Log($"{objectsToSave[i].name}");
            saveObject.gameObjectUniqueID = objectsToSave[i].GetComponent<GenerateGuid>().uniqueGuidID;
            var x = objectsToSave[i].GetComponents<Component>();
            var stateQueryComponent = x.Where(component => component is IStateQuery).ToList();
            List<KeyToValue> componentsState = new List<KeyToValue>();
            foreach (var z in stateQueryComponent)
            {
                var w = z as IStateQuery;
                componentsState.Add(new KeyToValue(w.UniqueId.ToString(), w.GetState()));
            }

            saveObject.transformSaver.position = objectsToSave[i].transform.position;
            saveObject.transformSaver.rotation = objectsToSave[i].transform.rotation;
            saveObject.transformSaver.scaling = objectsToSave[i].transform.localScale;

            saveObject.componentsState = componentsState;
            saveGame.saveObjects.Add(saveObject);
        }

        string json = JsonUtility.ToJson(saveGame);

        if (Folder == null && FileName == null)
        {
            SaveSystem.Save(json);
        }
        else
        {
            SaveSystem.Save(Folder, FileName, json);
        }
    }

    public void Load(string Folder, string FileName)
    {
       var objectsToLoad =  UpdateObjectsToSave();

        Dictionary<string, GameObject> uniqueIdToObject = objectsToLoad
            .ToDictionary(o => o.GetComponent<GenerateGuid>().uniqueGuidID, o => o);

        saveString = SaveSystem.Load(Folder, FileName);

        if (saveString != null)
        {
            SaveGame saveGame = JsonUtility.FromJson<SaveGame>(saveString);
            foreach (var saveObject in saveGame.saveObjects)
            {
                List<KeyToValue> loadedComponents = saveObject.componentsState;

                if (uniqueIdToObject.ContainsKey(saveObject.gameObjectUniqueID))
                {
                    var objectToSetState = uniqueIdToObject[saveObject.gameObjectUniqueID];

                    objectToSetState.transform.position = saveObject.transformSaver.position;
                    objectToSetState.transform.rotation = saveObject.transformSaver.rotation;
                    objectToSetState.transform.localScale = saveObject.transformSaver.scaling;

                    var y = objectToSetState.GetComponents<Component>();
                    var z = y.Where(component => component is IStateQuery).ToList();
                    Dictionary<string, IStateQuery> zz = z.ToDictionary(sq => (sq as IStateQuery).UniqueId.ToString(), sq => sq as IStateQuery);

                    foreach (KeyToValue keyvalue in loadedComponents)
                    {
                        zz[keyvalue.Key].SetState(keyvalue.Value);
                    }
                }
            }
        }
    }

    private List<GameObject> UpdateObjectsToSave()
    {
        var objectsWithGenerateGuid = GameObject.FindObjectsOfType<GenerateGuid>().ToList();
        var objectsToSave = new List<GameObject>();

        if (objectsWithGenerateGuid.Count > 0 && objectsToSave.Count == 0)
        {
            for (int i = 0; i < objectsWithGenerateGuid.Count; i++)
            {
                objectsToSave.Add(objectsWithGenerateGuid[i].gameObject);
            }
        }

        return objectsToSave;
    }

    public IEnumerator AuatomaticSaveWithTime()
    {
        yield return new WaitForSeconds(timeToStartSaving);

        Save(null, null);

        StartCoroutine(fadeInOutSaveGame.OverAllTime(savingFadeInOutTime));
    }

    public IEnumerator SaveWithTime(string Folder, string FileName)
    {
        yield return new WaitForSeconds(timeToStartSaving);

        Save(Folder, FileName);

        StartCoroutine(fadeInOutSaveGame.OverAllTime(savingFadeInOutTime));
    }
}

Depends on what you want, really.

The Visual Studio tool is meant to generate guid you are free to use for whatever you like. It isn't tied to your code in any way. You can of course use these values as static resources for your app, so they will always be the same at runtime.

The Guid.NewGuid will generate a new Guid value whenever your code runs. It will always be dynamic, even for the same object. You run the same app today and tomorrow, you'll get different guids.

Based on your comments, my understanding is that you really are looking for an easier way to inject static guids in your code editor to uniquely identify component types. If that's the case, have a look at the following extension:

https://marketplace.visualstudio.com/items?itemName=florians.InsertGUIDCommand

It will allow you to use SHIFT-ALT-G to inject a brand new guid value in your code. It should save you a few mouse clicks. ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM