Quantcast
Channel: Answers for "Default Values for Components"
Viewing all articles
Browse latest Browse all 6

Answer by ZorbaTHut

$
0
0
You can use the hierarchyWindowChanged callback to detect when something in the hierarchy has been added (which includes components), then change the values on a newly-added component. You have to be careful not to change things that already existed, and you have to be careful not to screw up prefabs, but it's doable. Because I'm too lazy to not provide example code, example code: using UnityEditor; using UnityEngine; using System; [InitializeOnLoad] public class DefaultSetter { static string m_LastScene; static Rigidbody[] m_LastSeenRigidbody; static void Autorun() { EditorApplication.hierarchyWindowChanged += ExampleCallback; } static void ExampleCallback () { if (m_LastScene != EditorApplication.currentScene) { m_LastSeenRigidbody = Resources.FindObjectsOfTypeAll(typeof(Rigidbody)) as Rigidbody[]; m_LastScene = EditorApplication.currentScene; } else { Rigidbody[] newRigidbody = Resources.FindObjectsOfTypeAll(typeof(Rigidbody)) as Rigidbody[]; foreach (Rigidbody rigidbody in newRigidbody) { if (!Array.Exists(m_LastSeenRigidbody, element => element == rigidbody)) { if (PrefabUtility.GetPrefabObject(rigidbody) == null) { Debug.LogFormat("Setting default rigidbody constraints on new rigidbody {0}", rigidbody); rigidbody.constraints = RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY; } } } m_LastSeenRigidbody = newRigidbody; } } } That's probably imperfect but it seems to work, for now; report any bugs and I'll fix 'em.

Viewing all articles
Browse latest Browse all 6

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>