示例#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;
        }
示例#2
0
        public bool Load(XMLRules.VariableType variable)
        {
            m_Name  = (string)variable.GetName().Value;
            m_Type  = (VariableType)variable.GetType2().Value;
            m_Value = GetValueFromConstant((VariableType)variable.GetType2().Value, variable.GetValue());

            if (variable.HasUpdateScript())
            {
                BaseScript updateScript;

                RemoveScript();

                if (BaseScript.Load(variable.GetUpdateScript(), m_GameObject, out updateScript))
                {
                    m_UpdateScriptType = updateScript.GetType();
                    m_UpdateScript     = (BaseSensor)updateScript;
                }
            }

            m_IsDirty = true;

            return(true);
        }
示例#3
0
        public static bool Save(BaseScript script, out ScriptReference xmlScriptReference)
        {
            xmlScriptReference = new ScriptReference();

            xmlScriptReference.AddScriptName(new SchemaString(script.GetType().ToString()));

            foreach (FieldInfo field in script.m_Fields)
            {
                ParameterType parameter = new ParameterType();

                parameter.AddName(new SchemaString(field.Name));
                parameter.AddType2(new SchemaString(field.FieldType.ToString()));

                object value = field.GetValue(script);
                if (value != null)
                    parameter.AddValue(Variable.GetConstantFromValue(value));
                else
                    Debug.LogError("Error saving value for field " + field.Name + " in script " + script.GetType().ToString());

                xmlScriptReference.AddParameter(parameter);
            }

            return true;
        }
示例#4
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);
        }