GameObject gameObject = GameObject.Instantiate(prefab, GameObject.Find(“Canvas”).transform);
where prefab is set as a suitably created Prefab, and Canvas is in the Scene.
But it doesn’t create any object…
I would really appreciate any help.
Sorry for edit
-Update-
I want to instantiate 11 GameObject Instances at once.
Data is stored at InitData instances.
[System.Serializable]
public class InitData
{
public GameObject prefab;
public Sprite sprite;
public Vector3 position;
public Vector2 sizeDelta;
public Vector3 scale;
public Quaternion rotation; // these should be protected or, initiated using InitData.ctor()
public virtual GameObject InitGO() { return null; }
public override bool Equals(object obj)
{
if (obj is InitData)
{
InitData id = (InitData)obj;
return id.prefab == prefab && id.position == position && id.sizeDelta == sizeDelta && id.scale == scale && id.rotation.x == rotation.x && id.rotation.y == rotation.y && id.rotation.z == rotation.z && id.rotation.w == rotation.w;
}
else
{
return false;
}
}
}
InitDataUI inherits InitData
public class InitDataUI : InitData
{
public InitDataUI(GameObject gameObject, GameObject prefab)
{
this.prefab = prefab;
Image image = gameObject.GetComponent<Image>();
RectTransform rectTransform = gameObject.GetComponent<RectTransform>();
sprite = image.sprite;
position = rectTransform.position;
sizeDelta = rectTransform.sizeDelta;
scale = rectTransform.localScale;
rotation = rectTransform.rotation;
}
public override GameObject InitGO()
{
GameObject gameObject = GameObject.Instantiate(prefab, GameObject.Find("Canvas").transform);
Image image = gameObject.GetComponent<Image>();
RectTransform rectTransform = gameObject.GetComponent<RectTransform>();
image.sprite = sprite;
rectTransform.position = position;
rectTransform.sizeDelta = sizeDelta;
rectTransform.localScale = scale;
rectTransform.rotation = rotation;
return gameObject;
}
}
Then ObjectInit.Init calls InitData.InitGO
public class ObjectsInit : Manager
{
public ListStringDictionary<InitData> initDatas;
public void Start()
{
}
public List<GameObject> Init(string name)
{
List<GameObject> retur = new();
List<InitData> ID = initDatas[name];
foreach (InitData data in ID)
{
retur.Add(data.InitGO());
}
return retur;
}
}
ObjectInit.Init is called by SceneSettings.Start
public class SceneSettings : Manager // 100 ExcOrder
{
public void Start()
{
blocksG = new(new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }, Get<ObjectsInit>().Init("BlocksG"));
blocksR = new(new List<int> { 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22 }, Get<ObjectsInit>().Init("BlocksR"));
}
}
Manager inherits MonoBehaviour
SceneSettings.Get returns Manager instance of type T
ObjectInit, SceneSettings are components attached to Managers
SceneSettings has Excution Order +100
But GameObject is not instantiated still…
-Update 2-
In addition, I have more than 50 GameObjects loaded each game.
So, I’m gonna use ObjectPool to instantiate these objects…