NodeCanvas Forums › Custom Nodes & Tasks › extended InstantiateGameObject action
Hello
I’ve extended the InstantiateGameObject action to include parent and rotation parameters.
Instantiating directly under the parent is faster than instantiating under the world and immediately parenting (it’s somewhere in the Unity3d blog, cannot find the link now).
Also helps by removing the need to add another action to do the parenting.
I’ve replaced the original InstantiateGameObject.cs with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using NodeCanvas.Framework; using ParadoxNotion.Design; using UnityEngine; namespace NodeCanvas.Tasks.Actions { [Category("GameObject")] public class InstantiateGameObject : ActionTask<Transform> { public BBParameter<Transform> parent; public BBParameter<Vector3> clonePosition; public BBParameter<Quaternion> cloneRotation = new BBParameter<Quaternion>(Quaternion.identity); [BlackboardOnly] public BBParameter<GameObject> saveCloneAs; protected override string info { get { return "Instantiate " + agentInfo + " under " + (parent.value ? parent.ToString() : "World") + " at " + clonePosition + " as " + saveCloneAs; } } protected override void OnExecute() { saveCloneAs.value = (GameObject)Object.Instantiate(agent.gameObject, clonePosition.value, cloneRotation.value, parent.value); EndAction(); } } } |
Hey,
Nice one. I will update the action as well, but I think I will make the rotation a Vector3 instead of Quaternion so that it is easier to adjust with sanity 🙂
To that end, I went ahead and added a new AutoCast conversion from Quaternion to Vector3 and vice verse, which means that the Vector3 parameter of this action (or any action) can be linked to a Quaternion Variable which will be auto converted to Vector3, like the rest of the AutoCasts work.
Thanks!
Join us on Discord: https://discord.gg/97q2Rjh
Modified it again, to include better info when parenting under a BB variable, and instantiating directly under the parent, instead of changing the hierarchy (a potentially expensive operation).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
using NodeCanvas.Framework; using ParadoxNotion.Design; using UnityEngine; namespace NodeCanvas.Tasks.Actions { [Category("GameObject")] public class InstantiateGameObject : ActionTask<Transform> { public BBParameter<Transform> parent; public BBParameter<Vector3> clonePosition; public BBParameter<Vector3> cloneRotation; [BlackboardOnly] public BBParameter<GameObject> saveCloneAs; protected override string info { get { string parentName; if (parent.value || parent.useBlackboard) parentName = parent.ToString(); else parentName = "World"; if (saveCloneAs.useBlackboard) return string.Format("Instantiate {0} under {1} at {2} as {3}", agentInfo, parentName, clonePosition, saveCloneAs); else return string.Format("Instantiate {0} under {1} at {2}", agentInfo, parentName, clonePosition); } } protected override void OnExecute() { var clone = Object.Instantiate(agent.gameObject, parent.value, false); clone.transform.position = clonePosition.value; clone.transform.eulerAngles = cloneRotation.value; saveCloneAs.value = clone; EndAction(); } } } |
Hey,
The reason I did not use the Instantiate overload that automatically parents the instance is because it’s not available in Unity 5.3 🙂
Join us on Discord: https://discord.gg/97q2Rjh
Hello
Haven’t though about that.
Simple to account for though:
1 2 3 4 5 6 7 8 |
#if UNITY_5_4_OR_NEWER var clone = Object.Instantiate(agent.gameObject, parent.value, false); #else var clone = (GameObject)Object.Instantiate(agent.gameObject); clone.transform.parent = parent.value; #endif |
The UNITY_X_Y_OR_NEWER was added in 5.3.4
I’ve read in the Unity dev blog that due to changes made in the internal transform hierarchy representation starting with 5.4 (if I’m not mistaken), the cost of re-parenting large hierarchies can be significant, and it also allocates and moves significant amounts of memory. They mentioned in particular the version of Instantiate that takes a parent vs instantiating and immediately setting the parent.
Yeah I know of the UNITY_X_Y_OR_NEWER directive. I just didnt think that it is such a performance difference.
Regardless, I’ve just modified the task such as you posted above.
Thanks.
Join us on Discord: https://discord.gg/97q2Rjh