Created
November 4, 2019 19:32
-
-
Save Cranc/07de22413c4be5295a30388a2696556c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GameObjectManager : MonoBehaviour, ISerializationCallbackReceiver | |
{ | |
private static GameObjectManager _instance; | |
public static GameObjectManager Instance { get { return _instance; } } | |
public List<GameObjectList> gameObjects; //public list so it can be accessed in inspector | |
private Dictionary<GameObject, Texture2D> gameObjectDictionary; //pricate list for use | |
private List<GameObject> placedGameObjects = new List<GameObject>(); | |
private void Awake() | |
{ | |
if (_instance != null && _instance != this) | |
{ | |
Destroy(this.gameObject); | |
} | |
else | |
{ | |
_instance = this; | |
} | |
} | |
/// <summary> | |
/// Maps the Dictionary into a list of Mapped Objects containing | |
/// name, id, and img | |
/// </summary> | |
/// <returns>List of GameObjectMapped</returns> | |
public List<GameObjectMapped> GetGameObjectsList() | |
{ | |
List<GameObjectMapped> map = new List<GameObjectMapped>(); | |
foreach(var entry in gameObjectDictionary) | |
{ | |
map.Add(new GameObjectMapped(entry.Key, entry.Key.name, entry.Value)); | |
} | |
return map; | |
} | |
/// <summary> | |
/// Creates a GameObject in the Scene | |
/// </summary> | |
/// <param name="id">ID of the GameObejct</param> | |
/// <param name="position">Position the Object should be placed at</param> | |
/// <param name="scale">Optional Scale of the Object default is (1,1,1)</param> | |
public GameObject CreateGameObjectAtById(GameObject id, Vector3 position, Vector3? scale = null) | |
{ | |
if (!gameObjectDictionary.ContainsKey(id)) | |
{ | |
Debug.LogError("CreateGameObjectAtById: ID not found"); | |
return null; | |
} | |
var obj = Instantiate(id, position, Quaternion.identity) as GameObject; | |
if (scale != null) | |
obj.transform.localScale = scale.Value; | |
placedGameObjects.Add(obj); | |
return obj; | |
} | |
/// <summary> | |
/// Removes a given Object from the Scene Deleting it. | |
/// only Destroys Objects created by GameObjectManager | |
/// </summary> | |
/// <param name="id">id of the Obejct to Destroy</param> | |
public void RemoveGameObjectById(GameObject id) | |
{ | |
var obj = placedGameObjects.Find(x => x.Equals(id)); | |
if (obj == null) | |
return; | |
placedGameObjects.Remove(obj); | |
Object.Destroy(obj); | |
} | |
/// <summary> | |
/// Callback stuff | |
/// </summary> | |
public void OnBeforeSerialize() | |
{ | |
gameObjects.Clear(); | |
foreach (var kvp in gameObjectDictionary) | |
{ | |
gameObjects.Add(new GameObjectList(kvp.Key, kvp.Value)); | |
} | |
} | |
/// <summary> | |
/// Callback stuff | |
/// </summary> | |
public void OnAfterDeserialize() | |
{ | |
gameObjectDictionary = new Dictionary<GameObject, Texture2D>(); | |
foreach (var entry in gameObjects) | |
{ | |
gameObjectDictionary.Add(entry.obj, entry.img); | |
} | |
} | |
//some debug | |
void OnGUI() | |
{ | |
#if DICTIONARY_DEBUG | |
foreach (var kvp in gameObjectDictionary) | |
GUILayout.Label("Key: " + kvp.Key + " value: " + kvp.Value); | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment