Example #1
0
        /// <summary>
        /// Resets the demo game and runs the tank with the specified C# code controlling it.
        /// </summary>
        /// <param name="source">The C# sorce code to run</param>
        public void RunTankScript(string source)
        {
            if (tankObject != null)
            {
                // Strip the old controller script
                TankController old = tankObject.GetComponent <TankController>();

                if (old != null)
                {
                    Destroy(old);
                }

                // Reposition the tank at its start position
                RespawnTank();
            }
            // Compile the script
            ScriptType type = domain.CompileAndLoadScriptSource(source);

            if (type == null)
            {
                Debug.Log("Compile failed" + domain.GetErrorLineValue());
                // Debug.LogError("Compile failed");
                return;
            }
            else
            {
                GameObject.Find("ErrorExplain").GetComponent <Text>().text    = "";
                GameObject.Find("ErrorShowLine").GetComponent <Image>().color = new Color(0, 0, 0, 0f);
            }

            // Make sure the type inherits from 'TankController'
            if (type.IsSubtypeOf <TankController>() == true && tankObject != null)
            {
                // Attach the component to the object
                proxy = type.CreateInstance(tankObject);

                // Check for error
                if (proxy == null)
                {
                    // Display error
                    Debug.LogError(string.Format("Failed to create an instance of '{0}'", type.RawType));
                    return;
                }
                // Assign the bullet prefab to the 'TankController' instance
                proxy.Fields["playBuSprite"]  = playBuSprite;
                proxy.Fields["buttonPlayimg"] = buttonPlayimg;
                proxy.Fields["bulletObject"]  = bulletObject;
                proxy.Fields["nextStage"]     = nextStage;
                // Call the run tank method   crash
                //proxy.Fields["crash"] = crash;
                proxy.Call("RunTank");
            }
            else
            {
                //if(tankObject != null){
                //    Debug.LogError("The script must inherit from 'TankController'");
                //}
            }
        }
        /// <summary>
        /// Resets the demo game and runs the tank with the specified C# code controlling it.
        /// </summary>
        /// <param name="source">The C# sorce code to run</param>
        public IEnumerator RunTankScript(string source)
        {
            // Strip the old controller script
            TankController old = tankObject.GetComponent <TankController>();

            if (old != null)
            {
                Destroy(old);
            }

            // Reposition the tank at its start position
            RespawnTank();

            // Compile the script asynchronously
            AsyncCompileLoadOperation task = domain.CompileAndLoadScriptSourcesAsync(source);

            // Wait for compile to complete
            yield return(task);

            if (task.IsSuccessful == false)
            {
                Debug.LogError("Compile failed");
                yield break;
            }

            // Get the main compiled type
            ScriptType type = task.MainType;

            // Make sure the type inherits from 'TankController'
            if (type.IsSubtypeOf <TankController>() == true)
            {
                // Attach the component to the object
                ScriptProxy proxy = type.CreateInstance(tankObject);

                // Check for error
                if (proxy == null)
                {
                    // Display error
                    Debug.LogError(string.Format("Failed to create an instance of '{0}'", type.RawType));
                    yield break;
                }

                // Assign the bullet prefab to the 'TankController' instance
                proxy.Fields["bulletObject"] = bulletObject;

                // Call the run tank method
                proxy.Call("RunTank");
            }
            else
            {
                Debug.LogError("The script must inherit from 'TankController'");
            }
        }
        /// <summary>
        /// Resets the demo game and runs the tank with the specified C# code controlling it.
        /// </summary>
        /// <param name="source">The C# sorce code to run</param>
        public void RunTankScript(string source)
        {
            // Strip the old controller script
            TankController old = tankObject.GetComponent <TankController>();

            if (old != null)
            {
                Destroy(old);
            }

            // Reposition the tank at its start position
            RespawnTank();

            // Compile the script
            ScriptType type = domain.CompileAndLoadScriptSource(source);

            if (type == null)
            {
                Debug.LogError("Compile failed");
                return;
            }

            // Make sure the type inherits from 'TankController'
            if (type.IsSubtypeOf <TankController>() == true)
            {
                // Attach the component to the object
                ScriptProxy proxy = type.CreateInstance(tankObject);

                // Check for error
                if (proxy == null)
                {
                    // Display error
                    Debug.LogError(string.Format("Failed to create an instance of '{0}'", type.RawType));
                    return;
                }

                // Assign the bullet prefab to the 'TankController' instance
                proxy.Fields["bulletObject"] = bulletObject;

                // Call the run tank method
                proxy.Call("RunTank");
            }
            else
            {
                Debug.LogError("The script must inherit from 'TankController'");
            }
        }