The writing of Untiy3d action management mechanism

Source: Internet
Author: User

Using Unity3d is a good choice for some visual forcing, but Unity3d has no cocos2d action management mechanism, such as COCOS2DX's Ccmoveto,ccscale action, Therefore, the author through encapsulating the action management to achieve similar COCOS2DX actionmanager.

The first thing you need to do is write a actionmanager to create, update, and remove all action. Writing code is implemented as follows:

Using Unityengine;
Using System.Collections;
Using System;

public class Actionmanager:monobehaviour {
Private ArrayList actionlist = null;
private static ActionManager instance = null;//Create a ActionManager singleton
public static ActionManager Instance
{
Get
{
Instance = UnityEngine.Object.FindObjectOfType (typeof (ActionManager)) as ActionManager;
if (instance = = null)
{
Gameobject obj = new Gameobject ("ActionManager");//Each scene has a ActionManager gameobject to manage all action
Instance = obj. AddComponent (typeof (ActionManager)) as ActionManager;
Instance. Initactionlist ();
}
return instance;
}
}
public void Initactionlist ()
{
ActionList = new ArrayList ();//Initialize All action containers
}
void Update () {
for (int i = 0; i < actionlist.count;i++)
{
Basicaction action = (basicaction) actionlist[i];
Action. Update ();
The IF (action.bcompleted)//bcomplete variable is used to determine whether the action is recycled
{
Actionlist.removeat (i);
i--;
}
}
}

public void Addmovebyaction (Gameobject obj, Vector3 movedirection, float movedistance,space relative, float usetime, Action CB)
{
Actionlist.add (New Moveby (obj, movedirection, movedistance,relative, Usetime, CB));
}

public void Addmovetoaction (Gameobject obj, Vector3 targetpos, Space relative, float usetime, Action CB)
{
Actionlist.add (New MoveTo (Obj,targetpos, Relative, Usetime, CB));
}

public void Removeactionbygameobject (Gameobject obj)//Remove action by Gameobject
{
foreach (Basicaction action in actionlist)
{
if (action.myObj.Equals (obj))
{
Actionlist.remove (action);
Break
}
}

}

public void Removeallaction ()//Clear All action

{
Actionlist.clear ();
}

void OnDestroy ()
{
Actionlist.clear ();
}

There are common member variables in all actions, so you need to write a base class function to reduce common member variables and functions, and write the base class of the action baseaction:

Using Unityengine;
Using System.Collections;

public class Basicaction
{
public bool bcompleted = false;//To determine if the action is complete
The gameobject of public gameobject myobj;//action function
Public basicaction () {}

public virtual void Update () {}//update function, subclass rewrite required
}

After writing the base class, you can start an action, the following is a relatively simple moveby action:

Using Unityengine;
Using System.Collections;
Using System;

public class Moveby:basicaction {//move through specific distances and directions
/*
* Movedirection Movement Direction
* Movedistance Moving distance
* Usetime use time, unit seconds
*/

Private Vector3 speed;
private float time;
private float deltatime = 0;
Private Space myrelative;
Private Action Mcb=null;
private float Deltadis = 0,speedlen,movedistance;
Private Vector3 Targetpos;
Public Moveby (gameobject Obj,vector3 movedirection,float movedistance,space relative,float useTime,Action CB)
{
This.myobj=obj;
THIS.MCB = CB;
This.myrelative = relative;
This.movedistance = movedistance;
This.time = Usetime;
Movedirection.normalize ();

if (myrelative = = Space.world)
This.targetpos = this.myObj.transform.position + (movedirection * movedistance);
Else
This.targetpos = this.myObj.transform.localPosition + (movedirection * movedistance);

Usetime *= (1/0.02f);
Speed = (movedirection * movedistance)/usetime;
Speedlen = Vector3.distance (Speed,vector3.zero);
}

public override void Update ()
{
if (myobj==null)
{
Bcompleted = true;
Return
}

Deltadis + = Speedlen;
if (Deltadis >= movedistance)
{
MyObj.transform.localPosition = targetpos;//Make sure the gameobject reaches the target point

if (MCB = null)//can callback callback after the action is completed
MCB ();
bcompleted = true;//If the current action is completed, the action tag can be recycled
}
Else
{
if (myrelative = = Space.world)
MyObj.transform.position = new Vector3 (myobj.transform.position.x + speed.x, MYOBJ.TRANSFORM.POSITION.Y + speed.y, Myobj.transform.position.z + speed.z);
Else
MyObj.transform.localPosition = new Vector3 (myobj.transform.localposition.x + speed.x, MYOBJ.TRANSFORM.LOCALPOSITION.Y + speed.y, myobj.transform.localposition.z + speed.z);
}
}
}

Examples of Use:

ActionManager.Instance.AddMoveByAction (Gameobject obj, Vector3 movedirection, float movedistance,space relative, FLOAT usetime,action CB)

The above is a relatively simple action package process, you can also implement a variety of action packages, such as Moveto,rotationto,rotationby and so on.

(Please specify the source when reproduced, from the blog Garden: Hemjohn)

The writing of Untiy3d action management mechanism

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.