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
 
 namespace Helios.Otto
 {
+    //put me in an Editor folder
     public class Otto
     {
         /// <summary>
Updated by Sam Gates

File Otto.cs Modified

  • Ignore whitespace
  • Hide word diff
 using System;
+using System.Collections.Generic;
+using System.Reflection;
 using UnityEngine;
 using UnityEditor;
+using UnityEngine.SceneManagement;
 
-//Add to an Editor folder
 namespace Helios.Otto
 {
     public class Otto
         /// 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"
         /// </summary>
-        [MenuItem("CONTEXT/MonoBehaviour/Auto Populate Variables")]
-        static void AutoPopulate(MenuCommand command)
+        [MenuItem("CONTEXT/MonoBehaviour/Auto Populate - All")]
+        static void AutoPopulateAll(MenuCommand command)
         {
             Debug.Log("[Otto]: Auto populating variables");
             var type = command.context.GetType();
             var fieldsSet = 0;
             for (int i = 0; i < fields.Length; i++)
             {
-                var assetName = fields[i].Name.Split('_');
-                var asset = AssetDatabase.FindAssets(String.Join(" ",assetName));
-                if (asset.Length > 0)
+                var assetName = String.Join(" ", fields[i].Name.Split('_'));
+                if (SearchAssets(command.context, assetName, fields[i]))
                 {
-                    var assetPath = AssetDatabase.GUIDToAssetPath(asset[0]);
-                    var loadedAsset = AssetDatabase.LoadAssetAtPath(assetPath, fields[i].FieldType);
-                    if (loadedAsset != null)
-                    {
-                        Undo.RecordObject(command.context,$"Set field {fields[i].Name}");
-                        fieldsSet++;
-                        fields[i].SetValue(command.context, loadedAsset);
-                    }
+                    fieldsSet++;
+                    continue;
                 }
+
+                if (SearchScene(command.context, assetName, fields[i])) fieldsSet++;
+            }
+            Debug.Log($"[Otto]: All done! I set {fieldsSet} fields");
+        }
+
+        [MenuItem("CONTEXT/MonoBehaviour/Auto Populate - Assets")]
+        static void AutoPopulateAssets(MenuCommand command)
+        {
+            Debug.Log("[Otto]: Auto populating variables");
+            var type = command.context.GetType();
+            Debug.Log($"[Otto]: Doing a kickflip over the {type.Name}");
+            var fields = type.GetFields();
+            var fieldsSet = 0;
+            for (int i = 0; i < fields.Length; i++)
+            {
+                var assetName = String.Join(" ", fields[i].Name.Split('_'));
+                if (SearchAssets(command.context, assetName, fields[i])) fieldsSet++;
+            }
+            Debug.Log($"[Otto]: All done! I set {fieldsSet} fields");
+        }
+
+        [MenuItem("CONTEXT/MonoBehaviour/Auto Populate - Scene")]
+        static void AutoPopulateScene(MenuCommand command)
+        {
+            Debug.Log("[Otto]: Auto populating variables");
+            var type = command.context.GetType();
+            Debug.Log($"[Otto]: Doing a kickflip over the {type.Name}");
+            var fields = type.GetFields();
+            var fieldsSet = 0;
+            for (int i = 0; i < fields.Length; i++)
+            {
+                var assetName = String.Join(" ", fields[i].Name.Split('_'));
+                if (SearchScene(command.context, assetName, fields[i])) fieldsSet++;
             }
             Debug.Log($"[Otto]: All done! I set {fieldsSet} fields");
         }
+
+        static bool SearchAssets(UnityEngine.Object target, string search, FieldInfo field)
+        {
+            var asset = AssetDatabase.FindAssets(search);
+            if (asset.Length > 0)
+            {
+                var assetPath = AssetDatabase.GUIDToAssetPath(asset[0]);
+                var loadedAsset = AssetDatabase.LoadAssetAtPath(assetPath, field.FieldType);
+                if (loadedAsset != null)
+                {
+                    Undo.RecordObject(target, $"Set field {field.Name}");
+                    field.SetValue(target, loadedAsset);
+                    return true;
+                }
+            }
+            return false;
+        }
+
+        static bool SearchScene(UnityEngine.Object target, string search, FieldInfo field)
+        {
+            //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);
+                }
+            }
+            object targetObject = obj.Find(x => x.name.Contains(search));
+
+            if (!field.FieldType.Name.Contains("GameObject"))
+            {
+                targetObject = ((GameObject)targetObject)?.GetComponent(field.FieldType);
+            }
+
+            if (targetObject != null)
+            {
+                Undo.RecordObject(target, $"Set field {field.Name}");
+                field.SetValue(target, targetObject);
+                return true;
+            }
+
+            return false;
+        }
+
+        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
 using UnityEditor;
 
 //Add to an Editor folder
-public class Otto
+namespace Helios.Otto
 {
-    /// <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"
-    /// </summary>
-    [MenuItem("CONTEXT/MonoBehaviour/Auto Populate Variables")]
-    static void AutoPopulate(MenuCommand command)
+    public class Otto
     {
-        Debug.Log("[Otto]: Auto populating variables");
-        var type = command.context.GetType();
-        Debug.Log($"[Otto]: Doing a kickflip over the {type.Name}");
-        var fields = type.GetFields();
-        var fieldsSet = 0;
-        for (int i = 0; i < fields.Length; i++)
+        /// <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"
+        /// </summary>
+        [MenuItem("CONTEXT/MonoBehaviour/Auto Populate Variables")]
+        static void AutoPopulate(MenuCommand command)
         {
-            var assetName = fields[i].Name.Split('_');
-            var asset = AssetDatabase.FindAssets(String.Join(" ",assetName));
-            if (asset.Length > 0)
+            Debug.Log("[Otto]: Auto populating variables");
+            var type = command.context.GetType();
+            Debug.Log($"[Otto]: Doing a kickflip over the {type.Name}");
+            var fields = type.GetFields();
+            var fieldsSet = 0;
+            for (int i = 0; i < fields.Length; i++)
             {
-                var assetPath = AssetDatabase.GUIDToAssetPath(asset[0]);
-                var loadedAsset = AssetDatabase.LoadAssetAtPath(assetPath, fields[i].FieldType);
-                if (loadedAsset != null)
+                var assetName = fields[i].Name.Split('_');
+                var asset = AssetDatabase.FindAssets(String.Join(" ",assetName));
+                if (asset.Length > 0)
                 {
-                    Undo.RecordObject(command.context,$"Set field {fields[i].Name}");
-                    fieldsSet++;
-                    fields[i].SetValue(command.context, loadedAsset);
+                    var assetPath = AssetDatabase.GUIDToAssetPath(asset[0]);
+                    var loadedAsset = AssetDatabase.LoadAssetAtPath(assetPath, fields[i].FieldType);
+                    if (loadedAsset != null)
+                    {
+                        Undo.RecordObject(command.context,$"Set field {fields[i].Name}");
+                        fieldsSet++;
+                        fields[i].SetValue(command.context, loadedAsset);
+                    }
                 }
             }
+            Debug.Log($"[Otto]: All done! I set {fieldsSet} fields");
         }
-        Debug.Log($"[Otto]: All done! I set {fieldsSet} fields");
     }
-}
+}
Updated by Sam Gates

File Otto.cs Modified

  • Ignore whitespace
  • Hide word diff
 using UnityEngine;
 using UnityEditor;
 
+//Add to an Editor folder
 public class Otto
 {
     /// <summary>
Created by Sam Gates

File Otto.cs Added

  • Ignore whitespace
  • Hide word diff
+using System;
+using UnityEngine;
+using UnityEditor;
+
+public class Otto
+{
+    /// <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"
+    /// </summary>
+    [MenuItem("CONTEXT/MonoBehaviour/Auto Populate Variables")]
+    static void AutoPopulate(MenuCommand command)
+    {
+        Debug.Log("[Otto]: Auto populating variables");
+        var type = command.context.GetType();
+        Debug.Log($"[Otto]: Doing a kickflip over the {type.Name}");
+        var fields = type.GetFields();
+        var fieldsSet = 0;
+        for (int i = 0; i < fields.Length; i++)
+        {
+            var assetName = fields[i].Name.Split('_');
+            var asset = AssetDatabase.FindAssets(String.Join(" ",assetName));
+            if (asset.Length > 0)
+            {
+                var assetPath = AssetDatabase.GUIDToAssetPath(asset[0]);
+                var loadedAsset = AssetDatabase.LoadAssetAtPath(assetPath, fields[i].FieldType);
+                if (loadedAsset != null)
+                {
+                    Undo.RecordObject(command.context,$"Set field {fields[i].Name}");
+                    fieldsSet++;
+                    fields[i].SetValue(command.context, loadedAsset);
+                }
+            }
+        }
+        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.