Snippets

Sam Gates Otto - Populate your public variables with a single click!

Updated by Sam Gates

File Otto.cs Modified

  • Ignore whitespace
  • Hide word diff
 
             if (!field.FieldType.Name.Contains("GameObject"))
             {
-                targetObject = ((GameObject)targetObject)?.GetComponent(field.FieldType);
+                if (field.FieldType.Name.Contains("UnityEngine.Transform")) targetObject = ((GameObject)targetObject).transform;
+                else targetObject = ((GameObject)targetObject)?.GetComponent(field.FieldType);
             }
 
             if (targetObject != null)
         static void GetChildren(ref List<GameObject> l, Transform t)
         {
             if (!l.Contains(t.gameObject)) l.Add(t.gameObject);
-            
+
             for (int childIndex = 0; childIndex < t.childCount; childIndex++)
             {
                 var child = t.GetChild(childIndex);
                 if (child.childCount > 0)
                 {
                     var r = new List<GameObject>();
-                    GetChildren(ref r,child);
-                    foreach(GameObject g in r) if(!l.Contains(g))l.Add(g);
+                    GetChildren(ref r, child);
+                    foreach (GameObject g in r) if (!l.Contains(g)) l.Add(g);
                 }
             }
         }
 
         static bool TypeNotSceneCompatible(Type type)
         {
-            return (!type.IsSubclassOf(typeof(MonoBehaviour)) && !type.IsSubclassOf(typeof(Behaviour)) && type.Name != "GameObject");
+            return (!type.IsSubclassOf(typeof(MonoBehaviour)) && !type.IsSubclassOf(typeof(Behaviour)) && type.Name != "GameObject" && !type.Name.Contains("Transform"));
         }
     }
 }
Updated by Sam Gates

File Otto.cs Modified

  • Ignore whitespace
  • Hide word diff
             return false;
         }
 
-        static List<GameObject> GetSceneObejcts()
-        {
-            List<GameObject> obj = new List<GameObject>();
-            SceneManager.GetActiveScene().GetRootGameObjects(obj);
-            var rootObj = new List<GameObject>(obj);
-            foreach (GameObject o in rootObj)
-            {
-                GetChildren(ref obj, o.transform);
-            }
-
-            return obj;
-        }
-
         static bool SearchScene(UnityEngine.Object target, string search, FieldInfo field, List<GameObject> obj)
         {
             //if we can't even find the obejct type in the scene, why search for it?
             return false;
         }
 
+        static List<GameObject> GetSceneObejcts()
+        {
+            List<GameObject> obj = new List<GameObject>();
+            SceneManager.GetActiveScene().GetRootGameObjects(obj);
+            var rootObj = new List<GameObject>(obj);
+            foreach (GameObject o in rootObj)
+            {
+                GetChildren(ref obj, o.transform);
+            }
+            return obj;
+        }
+
         static void GetChildren(ref List<GameObject> l, Transform t)
         {
             if (!l.Contains(t.gameObject)) l.Add(t.gameObject);
Updated by Sam Gates

File Otto.cs Modified

  • Ignore whitespace
  • Hide word diff
 using UnityEngine;
 using UnityEditor;
 using UnityEngine.SceneManagement;
+using System.Linq;
 
 namespace Helios.Otto
 {
             Debug.Log($"[Otto]: Doing a kickflip over the {type.Name}");
             var fields = type.GetFields();
             var fieldsSet = 0;
+            List<GameObject> sceneObjects = GetSceneObejcts();
             for (int i = 0; i < fields.Length; i++)
             {
                 var assetName = ParseName(fields[i].Name);
                     continue;
                 }
 
-                if (SearchScene(command.context, assetName, fields[i])) fieldsSet++;
+                if (SearchScene(command.context, assetName, fields[i], sceneObjects)) fieldsSet++;
             }
             Debug.Log($"[Otto]: All done! I set {fieldsSet} fields");
         }
             Debug.Log($"[Otto]: Doing a kickflip over the {type.Name}");
             var fields = type.GetFields();
             var fieldsSet = 0;
+            List<GameObject> sceneObjects = GetSceneObejcts();
             for (int i = 0; i < fields.Length; i++)
             {
                 var assetName = ParseName(fields[i].Name);
-                if (SearchScene(command.context, assetName, fields[i])) fieldsSet++;
+                if (SearchScene(command.context, assetName, fields[i], sceneObjects)) fieldsSet++;
             }
             Debug.Log($"[Otto]: All done! I set {fieldsSet} fields");
         }
             return false;
         }
 
-        static bool SearchScene(UnityEngine.Object target, string search, FieldInfo field)
+        static List<GameObject> GetSceneObejcts()
         {
-            //if we can't even find the obejct type in the scene, why search for it?
-            //i.e. you won't find a float or audio clip in the scene!
-            if (TypeNotSceneCompatible(field.FieldType)) return false;
-
             List<GameObject> obj = new List<GameObject>();
             SceneManager.GetActiveScene().GetRootGameObjects(obj);
             var rootObj = new List<GameObject>(obj);
             foreach (GameObject o in rootObj)
             {
-                for (int childIndex = 0; childIndex < o.transform.childCount; childIndex++)
-                {
-                    var t = o.transform.GetChild(childIndex);
-                    if (!obj.Contains(t.gameObject)) obj.Add(t.gameObject);
-                }
+                GetChildren(ref obj, o.transform);
+            }
+
+            return obj;
+        }
+
+        static bool SearchScene(UnityEngine.Object target, string search, FieldInfo field, List<GameObject> obj)
+        {
+            //if we can't even find the obejct type in the scene, why search for it?
+            //i.e. you won't find a float or audio clip in the scene!
+            if (TypeNotSceneCompatible(field.FieldType))
+            {
+                return false;
             }
-            object targetObject = obj.Find(x => x.name.Contains(search));
+
+            object targetObject = obj.Find(x =>
+            {
+                var s = search.Split(' ');
+                return s.All(x.name.Contains);
+            });
 
             if (!field.FieldType.Name.Contains("GameObject"))
             {
             return false;
         }
 
+        static void GetChildren(ref List<GameObject> l, Transform t)
+        {
+            if (!l.Contains(t.gameObject)) l.Add(t.gameObject);
+            
+            for (int childIndex = 0; childIndex < t.childCount; childIndex++)
+            {
+                var child = t.GetChild(childIndex);
+
+                if (!l.Contains(child.gameObject))
+                {
+                    l.Add(child.gameObject);
+                }
+
+                if (child.childCount > 0)
+                {
+                    var r = new List<GameObject>();
+                    GetChildren(ref r,child);
+                    foreach(GameObject g in r) if(!l.Contains(g))l.Add(g);
+                }
+            }
+        }
+
         static string ParseName(string name)
         {
             var parsedName = String.Join(" ", Regex.Split(name, @"(?<!^)(?=[A-Z])"));
Updated by Sam Gates

File Otto.cs Modified

  • Ignore whitespace
  • Hide word diff
 using System;
 using System.Collections.Generic;
 using System.Reflection;
+using System.Text.RegularExpressions;
 using UnityEngine;
 using UnityEditor;
 using UnityEngine.SceneManagement;
         /// <summary>
         /// Loops through all public fields on the target mono behaviour and searches for a relevant asset in the asset database
         /// Separate your search terms with an underscore, and being as specific as possible with your variable names helps Otto to find the right assets
-        /// Example: "Optimistic Unicorn.mp4" can be found with variable names "Optimistic", "Unicorn", or best of all: "Optimistic_Unicorn"
+        /// Example: "Optimistic Unicorn.mp4" can be found with variable names "Optimistic", "Unicorn", or best of all: "OptimisticUnicorn"
         /// </summary>
         [MenuItem("CONTEXT/MonoBehaviour/Auto Populate - All")]
         static void AutoPopulateAll(MenuCommand command)
             var fieldsSet = 0;
             for (int i = 0; i < fields.Length; i++)
             {
-                var assetName = fields[i].Name.Replace('_',' ');
+                var assetName = ParseName(fields[i].Name);
                 if (SearchAssets(command.context, assetName, fields[i]))
                 {
                     fieldsSet++;
             var fieldsSet = 0;
             for (int i = 0; i < fields.Length; i++)
             {
-                var assetName = fields[i].Name.Replace('_', ' ');
+                var assetName = ParseName(fields[i].Name);
                 if (SearchAssets(command.context, assetName, fields[i])) fieldsSet++;
             }
             Debug.Log($"[Otto]: All done! I set {fieldsSet} fields");
             var fieldsSet = 0;
             for (int i = 0; i < fields.Length; i++)
             {
-                var assetName = fields[i].Name.Replace('_', ' ');
+                var assetName = ParseName(fields[i].Name);
                 if (SearchScene(command.context, assetName, fields[i])) fieldsSet++;
             }
             Debug.Log($"[Otto]: All done! I set {fieldsSet} fields");
             return false;
         }
 
+        static string ParseName(string name)
+        {
+            var parsedName = String.Join(" ", Regex.Split(name, @"(?<!^)(?=[A-Z])"));
+            return parsedName.Replace('_', ' ');
+        }
+
         static bool TypeNotSceneCompatible(Type type)
         {
             return (!type.IsSubclassOf(typeof(MonoBehaviour)) && !type.IsSubclassOf(typeof(Behaviour)) && type.Name != "GameObject");
Updated by Sam Gates

File Otto.cs Modified

  • Ignore whitespace
  • Hide word diff
 
 namespace Helios.Otto
 {
-    //put me in an Editor folder
+    //add me to an Editor folder
     public class Otto
     {
         /// <summary>
             var fieldsSet = 0;
             for (int i = 0; i < fields.Length; i++)
             {
-                var assetName = String.Join(" ", fields[i].Name.Split('_'));
+                var assetName = fields[i].Name.Replace('_',' ');
                 if (SearchAssets(command.context, assetName, fields[i]))
                 {
                     fieldsSet++;
             var fieldsSet = 0;
             for (int i = 0; i < fields.Length; i++)
             {
-                var assetName = String.Join(" ", fields[i].Name.Split('_'));
+                var assetName = fields[i].Name.Replace('_', ' ');
                 if (SearchAssets(command.context, assetName, fields[i])) fieldsSet++;
             }
             Debug.Log($"[Otto]: All done! I set {fieldsSet} fields");
             var fieldsSet = 0;
             for (int i = 0; i < fields.Length; i++)
             {
-                var assetName = String.Join(" ", fields[i].Name.Split('_'));
+                var assetName = fields[i].Name.Replace('_', ' ');
                 if (SearchScene(command.context, assetName, fields[i])) fieldsSet++;
             }
             Debug.Log($"[Otto]: All done! I set {fieldsSet} fields");
  1. 1
  2. 2
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.