Beispiel #1
0
        public static bool Load(ScriptReference xmlScriptReference, GameObject gameObject, out BaseScript newScript)
        {
            bool result = true;

            newScript = null;

            if (xmlScriptReference != null && xmlScriptReference.HasScriptName())
            {
                string name = (string)xmlScriptReference.GetScriptName().Value;

                Type scriptType = Type.GetType(name);

                if (scriptType != null)
                {
                    newScript = gameObject.AddComponent(scriptType) as BaseScript;

                    for (int i = 0; i < xmlScriptReference.GetParameterCount(); i++)
                    {
                        ParameterType parameter = xmlScriptReference.GetParameterAt(i);

                        if (parameter.HasName())
                        {
                            string parameterName = parameter.GetName().Value;
                            string parameterType = parameter.GetType2().Value;
                            object target        = null;

                            if (parameterType == typeof(UnityEngine.GameObject).ToString())
                            {
                                if (!Variable.ObjectFromString(parameter.GetValue().GetString2().Value, out target))
                                {
                                    Debug.LogError("Script " + name + " references an object (" + parameter.GetValue().GetString2().Value + ") that was not found.");
                                }
                            }
                            else
                            {
                                target = Variable.GetValueFromConstant(parameter.GetValue());
                            }

                            if (!newScript.SetFieldValue(parameterName, target))
                            {
                                Debug.LogError("Script " + name + " failed to set a value for " + parameterName);
                            }
                        }
                    }
                }
                else
                {
                    Debug.LogError("Script (" + name + ") is referenced as a sensor or an action but was not found");
                    result = false;
                }
            }

            return(result);
        }