Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GameObjectFactory.cs 1.8KB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace Svelto.Context
  4. {
  5. public class GameObjectFactory : Factories.IGameObjectFactory
  6. {
  7. public GameObjectFactory()
  8. {
  9. _prefabs = new Dictionary<string, GameObject[]>();
  10. }
  11. public GameObject Build(string prefabName)
  12. {
  13. DesignByContract.Check.Require(_prefabs.ContainsKey(prefabName), "Svelto.Factories.IGameObjectFactory -prefab was not found:" + prefabName);
  14. var go = Build(_prefabs[prefabName][0]);
  15. GameObject parent = _prefabs[prefabName][1];
  16. if (parent != null)
  17. {
  18. Transform transform = go.transform;
  19. var scale = transform.localScale;
  20. var rotation = transform.localRotation;
  21. var position = transform.localPosition;
  22. parent.SetActive(true);
  23. transform.parent = parent.transform;
  24. transform.localPosition = position;
  25. transform.localRotation = rotation;
  26. transform.localScale = scale;
  27. }
  28. return go;
  29. }
  30. /// <summary>
  31. /// Register a prefab to be built later using a string ID.
  32. /// </summary>
  33. /// <param name="prefab">original prefab</param>
  34. virtual public GameObject Build(GameObject prefab)
  35. {
  36. var copy = Object.Instantiate(prefab) as GameObject;
  37. return copy;
  38. }
  39. public void RegisterPrefab(GameObject prefab, string prefabName, GameObject parent = null)
  40. {
  41. var objects = new GameObject[2];
  42. objects[0] = prefab; objects[1] = parent;
  43. _prefabs.Add(prefabName, objects);
  44. }
  45. Dictionary<string, GameObject[]> _prefabs;
  46. }
  47. }