/// <summary> /// Attach scripts in Unity to the asset root gameobject, and optionally /// invoke a function with an optional argument. /// </summary> /// <param name="scriptSet">A string with format: scriptname:function:msg[;scriptname:function:msg]</param> public static void AttachScriptWithInvokeFunction(string scriptSet, GameObject gameObject) { // Scripts will be attached to the given gameObject. // Multiple scripts can be attached, where each script string is delimted by semicolon. // Then if set, the function will be invoked on the script passing in the message. string expectedFormat = "scriptname:function:argument[;scriptname:function:argument]"; string[] scriptLists = scriptSet.Split(';'); foreach(string scriptToAttach in scriptLists) { int scriptColon = scriptToAttach.IndexOf(":"); string scriptTypeName = scriptColon > 0 ? scriptToAttach.Substring(0, scriptColon).Trim() : scriptToAttach; System.Type scriptType = HEU_GeneralUtility.GetSystemTypeByName(scriptTypeName); if (scriptType == null) { Debug.LogFormat("Script with name {0} not found! Unable to attach script from attribute: {1}. Expected format: {2}", scriptTypeName, scriptToAttach, expectedFormat); return; } Component component = gameObject.GetComponent(scriptType); if (component == null) { Debug.LogFormat("Attaching script {0} to gameobject", scriptType); component = gameObject.AddComponent(scriptType); if (component == null) { Debug.LogFormat("Unable to attach script component with type '{0}' from script attribute: {1}", scriptType.ToString(), scriptToAttach); return; } } if (scriptColon + 1 >= scriptToAttach.Length) { // No function return; } int functionNameLength = 0; int functionColon = scriptToAttach.IndexOf(":", scriptColon + 1); functionNameLength = (functionColon > 0) ? functionColon - (scriptColon + 1) : scriptToAttach.Length - (scriptColon + 1); if (functionNameLength > 0) { string scriptFunction = scriptToAttach.Substring(scriptColon + 1, functionNameLength).Trim(); if (functionColon + 1 < scriptToAttach.Length) { // Get argument string scriptArgument = scriptToAttach.Substring(functionColon + 1).Trim(); //Debug.LogFormat("Invoking script function {0} with argument {1}", scriptFunction, scriptArgument); component.SendMessage(scriptFunction, scriptArgument, SendMessageOptions.DontRequireReceiver); } else { // No argument //Debug.LogFormat("Invoking script function {0}", scriptFunction); component.SendMessage(scriptFunction, SendMessageOptions.DontRequireReceiver); } } } }
/// <summary> /// Attach a script in Unity to the asset root gameobject, and optionally /// invoke a function with an optional argument. /// </summary> /// <param name="scriptToAttach">A string with format: scriptname:function:msg</param> public void AttachScriptWithInvokeFunction(string scriptToAttach) { // Script will be attached to the asset root. // Then if set, te function will be invoked on the script passing in the message. // Format: scriptname:function:msg string expectedFormat = "scriptname:function:argument"; int scriptColon = scriptToAttach.IndexOf(":"); if (scriptColon <= 0) { Debug.LogFormat("Invalid script attribute value. Expected format {0}, but got {1}", expectedFormat, scriptToAttach); return; } string scriptTypeName = scriptToAttach.Substring(0, scriptColon).Trim(); System.Type scriptType = HEU_GeneralUtility.GetSystemTypeByName(scriptTypeName); if (scriptType == null) { Debug.LogFormat("Script with name {0} not found! Unable to attach script from attribute: {1}", scriptTypeName, scriptToAttach); return; } GameObject rootGameObject = ParentAsset.RootGameObject; Component component = rootGameObject.GetComponent(scriptType); if (component == null) { Debug.LogFormat("Attaching script {0} to root gameobject", scriptType); component = rootGameObject.AddComponent(scriptType); if (component == null) { Debug.LogFormat("Unable to attach script component with type '{0}' from script attribute: {1}", scriptType.ToString(), scriptToAttach); return; } } if (scriptColon + 1 >= scriptToAttach.Length) { // No function return; } int functionColon = scriptToAttach.IndexOf(":", scriptColon + 1); int functionNameLength = functionColon - (scriptColon + 1); if (functionNameLength > 0) { string scriptFunction = scriptToAttach.Substring(scriptColon + 1, functionNameLength).Trim(); if (functionColon + 1 < scriptToAttach.Length) { // Get argument string scriptArgument = scriptToAttach.Substring(functionColon + 1).Trim(); //Debug.LogFormat("Invoking script function {0} with argument {1}", scriptFunction, scriptArgument); component.SendMessage(scriptFunction, scriptArgument, SendMessageOptions.DontRequireReceiver); } else { // No argument //Debug.LogFormat("Invoking script function {0}", scriptFunction); component.SendMessage(scriptFunction, SendMessageOptions.DontRequireReceiver); } } }