本文主要是介绍unity基础开发----Unity3D的uniSWF插件动态加载SWF UI资源,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
uniSWF能把Flash的素材像用AS3编程类似,只不过环境要在C#或者JS中编程。要是想创建一个类似Menu菜单或者像导航一样的菜单,在Unity中首先要选择摄像机,给摄像机添加MovieClipOverlayCameraBehaviour:
因为之后 MovieClipOverlayCameraBehaviour.instance中才包含stage对象的实例,然后需要选择相机的类型:
不然程序就会报错了。
然后我们就可以在程序中编写了,这个程序需要拖放的对象当然也是相机对象,要切记!动态载入和直接给Flash材质有区别需要找的对象放在swf路径的后面:
MovieClip minimap = new MovieClip("libufan/swf/UI2.swf:MCArea");
要想获得对象的子对象,可以采用getChildByName来获取:
playerArea = minimap.getChildByName<MovieClip>("roundArea");
btn.getChildByName<TextField>("txt")
当然要是想添加点击事件的话就和AS3中一样了,可以直接添加MouseEvent。下面是所有的程序,以备时间长了回来看看:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using pumpkin.display;
using pumpkin.events;
using pumpkin.tweener;
using pumpkin.text;public class Test1 : MonoBehaviour {private Stage stage;private MovieClip playerArea;private MovieClip btnContainer;private Dictionary<string,Handler> menus;public float scaleRatio = 0.5f;public float angle = 0f;private delegate void Handler();void Start () {if(!MovieClipOverlayCameraBehaviour.instance)return;stage = MovieClipOverlayCameraBehaviour.instance.stage;MovieClip minimap = new MovieClip("libufan/swf/UI2.swf:MCArea");playerArea = minimap.getChildByName<MovieClip>("roundArea");minimap.scaleX = minimap.scaleY = scaleRatio;minimap.x = stage.stageWidth-minimap.width/2*scaleRatio-20f;minimap.y = minimap.height/2*scaleRatio+20f;stage.addChild(minimap);BuildMenu();}void Update () {angle += Time.deltaTime*12f;playerArea.rotation = angle;}void MenuData(){menus = new Dictionary<string, Handler>();menus.Add("New Game", HandlerStart);menus.Add("Continue", HandlerStart);menus.Add("Options", HandlerStart);menus.Add("Exit",HandlerStart);}void BuildMenu(){MenuData();btnContainer = new MovieClip();stage.addChild(btnContainer);int i=0;foreach(KeyValuePair<string,Handler> keyvalue in menus){MovieClip btn = new MovieClip("libufan/swf/UI2.swf:MCTxt");btn.gotoAndStop(1);btn.looping = false;btn.mouseChildrenEnabled = false;btn.y = i*(btn.height+10f);btn.name = keyvalue.Key;//btn.getChildByName<TextField>("txt").text = btn.name;stage.addChild(btn);i++;}btnContainer.x = stage.stageWidth*0.5f;btnContainer.y = (stage.stageHeight - btnContainer.height)*0.5f;}void HandlerStart(){Debug.Log("start");}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using pumpkin.display;
using pumpkin.events;
using pumpkin.tweener;
using pumpkin.text;public class L2 : MonoBehaviour {private Stage stage;private MovieClip minimap;private MovieClip background;private MovieClip playerArea;private MovieClip btnContainer;private Dictionary<string,Handler> menus;public float scaleRatio=0.5f;public float angle=0f;private delegate void Handler();void Start () {if(!MovieClipOverlayCameraBehaviour.instance)return;stage=MovieClipOverlayCameraBehaviour.instance.stage;background=new MovieClip("learnUniSwf/1/hud2.swf:Background");[color=#008ef1]//背景全屏制作方法[/color]background.x=-2;background.y=-2;background.scaleX=(stage.stageWidth+10)/background.width;background.scaleY=(stage.stageHeight+10)/background.height;stage.addChild(background);minimap=new MovieClip("learnUniSwf/1/hud2.swf:Minimap");playerArea=minimap.getChildByName<MovieClip>("player");minimap.scaleX=minimap.scaleY=scaleRatio;minimap.x=stage.stageWidth-minimap.width/2*scaleRatio-20f;minimap.y=minimap.height/2*scaleRatio+20f;stage.addChild(minimap);BuildButtons();}void Update () {angle+=Time.deltaTime*12f;
//动画,模拟minimap 扫描playerArea.rotation=angle;}void MenuData(){menus=new Dictionary<string, Handler>();menus.Add("New Game",HandlerStart);menus.Add("Continue",HandlerContinue);menus.Add("Options",HandlerOptions);menus.Add("Exit",HandlerExit);}void BuildButtons(){MenuData();btnContainer=new MovieClip("learnUniSwf/1/hud2.swf:SimpleContainer");stage.addChild(btnContainer);int i=0;foreach(KeyValuePair<string,Handler> keyvalue in menus){MovieClip btn=new MovieClip("learnUniSwf/1/hud2.swf:SimpleButton");btn.gotoAndStop(1);btn.looping=false
//按钮子元素对Mouse 不触发事件,很关键,不然下文evt.target将指向Btn内的某元素btn.mouseChildrenEnabled=false;btn.y=i*(btn.height+10f);btn.name=keyvalue.Key;btn.getChildByName<TextField>("txt").text=btn.name;btnContainer.addChild(btn);i++;}btnContainer.x=stage.stageWidth/2;btnContainer.y=(stage.stageHeight-btnContainer.height)/2;
//对按钮父容器进行监听,通过事件的target引用找到按钮btnContainer.addEventListener(MouseEvent.CLICK,ClickHandler);btnContainer.addEventListener(MouseEvent.MOUSE_ENTER,OnEnter);btnContainer.addEventListener(MouseEvent.MOUSE_LEAVE,OnLeave);}void HandlerStart(){Debug.Log("handler start....");}void HandlerContinue(){Debug.Log("handler continue....");}void HandlerOptions(){Debug.Log("handler options....");}void HandlerExit(){Debug.Log("handler exit...."); }void ClickHandler(CEvent evt){Handler h=menus[(evt.target as MovieClip).name];if(h!=null){h();}}void OnEnter(CEvent evt){(evt.target as MovieClip).gotoAndPlay(1); }void OnLeave(CEvent evt){//(evt.target as MovieClip).gotoAndStop(1);}}
这篇关于unity基础开发----Unity3D的uniSWF插件动态加载SWF UI资源的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!