Use ECMASCRIPT4 (ACTIONSCRIPT3) to achieve Unity's hot update--using Fairygui (i)

Source: Internet
Author: User
Tags image filter

Our hot update scripts are used in practice and, of course, to support commonly used third-party components, such as a very useful third-party UI library: Fairygui.

What is Fairygui

Here Copy the Faiygui official website introduction:

Redefining the UI authoring process, full visualization, 0 code, is a UI authoring tool for programmers, designers, and game planners.
With FAIRYGUI-SDK, the performance of each game engine can be efficiently rendered, and the unique fairybatching technology automatically optimizes the number of DCs in a complex interface.

We are naturally using its unity components here.

The main thrust of our article is not to introduce how to use the Fairygui to make the interface, but to let the Fairygui-made interface be driven by our hot scripting.

So here we can skip the Fairygui editor and download the Unity demo it offers to perform a retrofit demonstration of the hot update.

Download the Fairygui Unity component here:

Import Fairygui
    1. Create a new Unity project.
    2. Import the Fairygui package in.
    3. Enter the Assets->fairygui->examples->scenes. This shows the full demo of Fairygui.

We'll show you the simplest hot update processing this time, so let's start with an example of a component that doesn't need to inherit and extend Fairygui in the script.

We chose example 23,2d image filter demo.

Prepare for hot updates
    1. First we save this scene as F_filter.
    2. Import the Unity plug-in package for the ACTIONSCRIPT3 virtual machine and generate the Hot update project. If you don't know how to do this, you can check here
    3. ACTIONSCRIPT3 Plugin Package has been updated, please download the latest plug-in package V0.96f6 and later version.

Scenario Analysis:

    • The UIPanel object in the scene, which hosts the UI interface.
    • There is a FilterMain.cs script on the UIPanel object that controls the logic of the UI.
    • Now we are trying to change the logic of this scenario to a hot update through the ACTIONSCRIPT3 script.

Operation Steps:

    1. Remove the FilterMain.cs. We will operate the logic in ACTIONSCRIPT3.
    2. The UIPanel (Script) under the UIPanel object is set to the disabled state. We'll re-enable it after the AS3 interpreter is initialized.
    3. Go to Assets->asruntimeplayer and drag As3player presets and as3startupprogress presets onto the scene.
    4. Set the document Class of the action script Start up Script component under the As3player object to filter.

Now we are porting the contents of the C # script into the ACTIONSCRIPT3 script.

Original demo in the Start method, a handler is added to the slider event on the UI, and then the filter parameters of the UI element are changed in the handler. See Code:

voidStart () {_mainview= This. Getcomponent<uipanel>(). UI; Blurfilter Blurfilter=NewBlurfilter (); Blurfilter.blursize=2; _mainview.getchild ("N21"). Filter =Blurfilter; _s0= _mainview.getchild ("S0"). Asslider; _S1= _mainview.getchild ("S1"). Asslider; _s2= _mainview.getchild ("S2"). Asslider; _S3= _mainview.getchild ("S3"). Asslider; _S4= _mainview.getchild ("S4"). Asslider; _s0.value= -; _s1.value= -; _s2.value= -; _s3.value= $; _s4.value= -;        _s0.onchanged.add (__sliderchanged);        _s1.onchanged.add (__sliderchanged);        _s2.onchanged.add (__sliderchanged);        _s3.onchanged.add (__sliderchanged);    _s4.onchanged.add (__sliderchanged); }    void__sliderchanged (Eventcontext context) {intCNT =_mainview.numchildren;  for(inti =0; I < CNT; i++) {GObject obj=_mainview.getchildat (i); if(Obj.filter iscolorfilter) {Colorfilter filter=(Colorfilter) Obj.filter; Filter.                Reset (); Filter. Adjustbrightness ((float) (_s0.value- -) /100f); Filter. Adjustcontrast ((float) (_s1.value- -) /100f); Filter. Adjustsaturation ((float) (_s2.value- -) /100f); Filter. Adjusthue ((float) (_s3.value- -) /100f); }            Else if(Obj.filter isblurfilter) {Blurfilter filter=(Blurfilter) Obj.filter; Filter.blursize= (float) _s4.value/ -; }        }    }

Now we'll rewrite this logical script as the ACTIONSCRIPT3 hot update script:

  1. Open the Hot update project and create a new filter class.
  2. Copy the following code into the filter class.
    Package {/** * ... * @author*/     Public classFilter { Public functionFilter () {}}}ImportFairygui. Blurfilter;ImportFairygui. Colorfilter;ImportFairygui. Eventcontext;ImportFairygui. gcomponent;ImportFairygui. GObject;ImportFairygui. Gslider;ImportFairygui. Stage;ImportFairygui. Uipackage;ImportFairygui. UIPanel;ImportUnityengine. Application;ImportUnityengine. Gameobject;ImportUnityengine. Monobehaviour;classFiltermainextendsmonobehaviour{var_mainview:gcomponent; var_s0:gslider; var_s1:gslider; var_s2:gslider; var_s3:gslider; var_s4:gslider; functionAwake () {application.targetframerate= 60; uipackage.addpackage______ ("Ui/filter"); }    functionStart () {_mainview= UIPanel ( This. Getcomponent (UIPanel)). UI; varBlurfilter:blurfilter=NewBlurfilter (); Blurfilter.blursize= 2; _mainview.getchild ("N21"). Filter =Blurfilter; _s0= _mainview.getchild ("S0"). Asslider; _S1= _mainview.getchild ("S1"). Asslider; _s2= _mainview.getchild ("S2"). Asslider; _S3= _mainview.getchild ("S3"). Asslider; _S4= _mainview.getchild ("S4"). Asslider; _s0.value= 100; _s1.value= 100; _s2.value= 100; _s3.value= 200; _s4.value= 20;        _s0.onchanged.add (__sliderchanged);        _s1.onchanged.add (__sliderchanged);        _s2.onchanged.add (__sliderchanged);        _s3.onchanged.add (__sliderchanged);    _s4.onchanged.add (__sliderchanged); }    function__sliderchanged (Context:eventcontext):void    {        varCnt:int =_mainview.numchildren;  for(varI:int = 0; I < CNT; i++)        {            varObj:gobject =_mainview.getchildat (i); if(Obj.filter is colorfilter) {varFilter1:colorfilter=Colorfilter (Obj.filter);                Filter1.reset (); Filter1.adjustbrightness ((_s0.value-100)/100); Filter1.adjustcontrast ((_s1.value-100)/100); Filter1.adjustsaturation ((_s2.value-100)/100); Filter1.adjusthue ((_s3.value-100)/100); }            Else if(Obj.filter is blurfilter) {varFilter2:blurfilter =Blurfilter (Obj.filter); Filter2.blursize= _s4.value/100; }}}}monobehaviour (Gameobject.find ("UIPanel"). Getcomponent (UIPanel)). Enabled =true; Gameobject.find ("UIPanel"). AddComponent (Filtermain);

  3. We see AS3 Code and C # Basic changes are not very small, almost direct copy copy and then change. Focus on the last two lines: we activated the UIPanel in the out-of-package code and mounted the Filtermain class defined in the script.
  4. Click Compile.
  5. Click Play in Unity. We see that the script has successfully taken effect.

Use ECMASCRIPT4 (ACTIONSCRIPT3) to achieve Unity's hot update--using Fairygui (i)

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.