Use ECMASCRIPT4 (ACTIONSCRIPT3) to achieve Unity's hot update--using Monobehaviour in scripts

Source: Internet
Author: User

Following the last analysis of the demo of the hot update, this time we will show you how to use Monobehaviour in the hot update code.

Monobehaviour the base class of the script that is mounted on the Gameobject object. In common unity development, it is simple to write a script that inherits from Monobehaviour, mounts it to the gameobject to manipulate, and gives it various behaviors.

Of course, in some large projects, the way to export hanging scripts may not be recommended, but this article is intended to show how to inherit and write Monobehaviour in a script, temporarily ignoring this problem.

Objectives of this chapter:

Create a monobehaviour with a script. and mount it to an object, in the Start method, use the HTML of the preempted Baidu home page, and then display its HTML code on the text box.

Operation Steps:
  1. Create a new Unity Hot update Project, or open the last project. If you don't know how to create a new one, refer to the previous article or crossing web
  2. Open the HotFixDemoScene1 and change the text box to a larger size.

  3. Open the Hot update project.
  4. If you are using a later version of Unity2017.2, you need to be aware of the following issues:Unityengine.www This class is used to download data from the network. But since Unity2017.2, Unity has split its API into many other DLLs. The default system only exports the UnityEngine.CoreModule.dll API, but Unityengine.www is not included in this DLL.
    1. You can use the following methods to determine which DLL a class is in: In VS, write the name of the class you want to determine.
    2. Right-click on the class name you just wrote, and in the pop-up menu, select the Quick glance definition to see the DLL that this type is located in.

    3. Here we're positioned in UnityEngine.UnityWebRequestWWWModule.dll.
    4. Open the Genapi.config.xml of the thermal project and configure the DLL in the following configuration section:
      <!--Configure DLLs to export -  <Buildassemblys>       <Assemblyvalue= "D:\Program files\unity\editor\data\mono\lib\mono\2.0\system.dll"></Assembly>    <Assemblyvalue= "D:\Program files\unity\editor\data\managed\unityengine\unityengine.coremodule.dll"></Assembly>    <Assemblyvalue= "D:\Program files\unity\editor\data\managed\unityengine\unityengine.unitywebrequestwwwmodule.dll"></Assembly>    <Assemblyvalue= "D:\Program files\unity\editor\data\unityextensions\unity\guisystem\unityengine.ui.dll"></Assembly>    <Assemblyvalue= "F:/astool_unitytest/as3hotfix_www\library\scriptassemblies\assembly-csharp.dll"></Assembly>      </Buildassemblys>
    5. Execute Bat/createunityapi.bat and build the Unity API again. This enables the use of WWW-related types in hot-update projects
  5. Open the main.as for the Hot update project.
  6. This time we write the function in the out-of-package code, so the main type of code is emptied, and then the following code is pasted:
    1  Package2 {3     4 [Doc]5     /**6      * ...7 * @author8      */9      Public classMainTen     { One  A          Public functionMain () -         { -         } the          -          Public functionUpdate ():void -         { -              +         }         -     } +      A } at  - //code that is written outside the package is executed once before the class of main is instantiated for the first time.  -  - ImportSystem.collections.IEnumerator; - ImportUnityengine. Camera; - ImportUnityengine. Gameobject; in ImportUnityengine. Monobehaviour; - ImportUnityengine. Time; to ImportUnityengine. WWW; + ImportUnityengine.ui.Text; -  the //defines a type that inherits from Monobehaviour.  * classBaidutestextendsMonobehaviour $ {Panax Notoginseng     //the Start method is called once before the update.  -     functionStart (): IEnumerator the     { +         //Create a WWW object and access www.baidu.com A         varWww:www =NewWWW ("Http://www.baidu.com"); the          while(    !Www.isDone) +         { -             //updates a UI element. $Text (Gameobject.find ("Text"). Getcomponent (text)). Text ="Loading from Baidu"+Time.framecount; $             //using the yield keyword, you can use the Unity coprocessor -Yieldreturnwww; -         } the          -         Wuyi         //when the load is complete, the HTML content is displayed to the UI.  theText (Gameobject.find ("Text"). Getcomponent (text)). Text = Www.text.substr (1024,1024); -         //destroys the WWW object.  Wu www.dispose (); -          About     } $      - } -  - //mount the baidutest defined in the script on the main camera.  ACamera.main.gameObject.addComponent (baidutest);

  7. See note, we define a monobehaviour subclass directly in the script and mount it on the camera.
  8. Execution Result:

This enables the use of monobehaviour in scripts.

Use ECMASCRIPT4 (ACTIONSCRIPT3) to achieve Unity's hot update--using Monobehaviour in scripts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.