使用unity3d对于一些可视化强迫者来说,是一个不错的选择,但unity3d没有cocos2d的action管理机制,比如cocos2dx的CCMoveTo,CCScale等action,所以笔者通过封装action管理来实现类似cocos2dx的actionmanager。

首先需要写一个ActionManager来创建、更新、移除所有action。编写代码实现如下:

using UnityEngine;
using System.Collections;
using System;

public class ActionManager : MonoBehaviour {
    private ArrayList actionList = null;
    private static ActionManager instance = null;//创建一个ActionManager单例
    public static ActionManager Instance
    {
        get
        {
            instance = UnityEngine.Object.FindObjectOfType(typeof(ActionManager)) as ActionManager;
            if (instance == null)
            {
                GameObject obj = new GameObject("ActionManager");//每个场景都有一个ActionManager的gameobject来管理所有Action
                instance = obj.AddComponent(typeof(ActionManager)) as ActionManager;
                instance.InitActionList();
            }
            return instance;
        }
    }
    public void InitActionList()
    {
        actionList = new ArrayList();//初始化所有Action容器
    }
    void Update () {
        for (int i = 0; i < actionList.Count;i++ )
        {
            BasicAction action = (BasicAction)actionList[i];
            action.Update();
            if(action.bCompleted)//bComplete变量用于判断action是否回收
            {
                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)//通过gameobject来移除action
    {
        foreach (BasicAction action in actionList)
        {
            if (action.myObj.Equals(obj))
            {
                actionList.Remove(action);
                break;
            }
        }
    
    }

public void RemoveAllAction()//清除所有action

{
        actionList.Clear();
    }

void OnDestroy()
    {
        actionList.Clear();
    }

在所有action中具有共同的成员变量,所以需要编写基类函数来减少共同的成员变量和函数,编写action的基类BaseAction:

using UnityEngine;
using System.Collections;

public class BasicAction
{
    public bool bCompleted = false;//用于判断action是否完成
    public GameObject myObj;//action作用的gameobject
    public BasicAction(){}

public virtual void Update(){}//update函数,需要子类重写
}

写完基类,就能入手一个action了,以下是一个比较简单的moveby action:

using UnityEngine;
using System.Collections;
using System;

public class MoveBy : BasicAction {         //通过具体距离和方向移动
    /*
     * moveDirection    移动方向
     *  moveDistance    移动距离
     *  useTime         使用的时间,单位秒
     */
     
    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;//确保gameobject到达目标点

if (mcb != null)//完成action后可回调callback
                mcb();
            bCompleted = true;//如果完成当前action,把action标记可以回收
        }
        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);
        }
    }
}

使用例子:

ActionManager.Instance.AddMoveByAction(GameObject obj, Vector3 moveDirection, float moveDistance,Space relative, float useTime,Action cb)

以上时一个比较简单的action封装过程,你也可以通过实现多种action封装,比如moveto,rotationto,rotationby等等。

(转载时请注明出处,from 博客园:HemJohn)

untiy3d action管理机制的编写的更多相关文章

  1. 【Cocos2d-x 3.x】内存管理机制与源码分析

    侯捷先生说过这么一句话 :  源码之前,了无秘密. 要了解Cocos2d-x的内存管理机制,就得阅读源码. 接触Cocos2d-x时, Cocos2d-x的最新版本已经到了3.2的时代,在学习Coco ...

  2. IOS- 内存管理机制

    iOS平台内存常见问题 作为iOS平台的开发者,是否曾经为内存问题而苦恼过?内存莫名的持续增长,程序莫名的crash,难以发现 的内存泄漏,这些都是iOS平台内存相关的常见问题:本文将会详细介绍iOS ...

  3. Android内存进程管理机制

    参考文章: http://www.apkbus.com/android-104940-1-1.htmlhttp://blog.sina.com.cn/s/blog_3e3fcadd0100yjo2.h ...

  4. 正确认识Android的内存管理机制,合理关闭进程 (一)

    随着大家收货后会有很多乐粉晒内存,为啦方便大家,在网上搜集了一些相关Andriod管理的相关机制合理管理内存,整理下发个贴. 首先要知道Android系统是基于Linux 2.6内核开发的开源操作系统 ...

  5. IOS中内存管理机制浅解

    我们知道在程序运行过程中要创建大量的对象,和其他高级语言类似,在ObjC中对象时存储在堆中的,系统并不会自动释放堆中的内存(注意基本类型是 由系统自己管理的,放在栈上).如果一个对象创建并使用后没有得 ...

  6. JVM介绍&自动内存管理机制

    1.介绍JVM(Java Virtual Machine,Java虚拟机) JVM是Java Virtual Machine的缩写,通常成为java虚拟机,作为Java可以进行一次编写,到处执行(Wr ...

  7. Spark内存管理机制

    Spark内存管理机制 Spark 作为一个基于内存的分布式计算引擎,其内存管理模块在整个系统中扮演着非常重要的角色.理解 Spark 内存管理的基本原理,有助于更好地开发 Spark 应用程序和进行 ...

  8. Android包管理机制(一) PackageInstaller的初始化

    前言 包管理机制是Android中的重要机制,是应用开发和系统开发需要掌握的知识点之一. 包指的是Apk.jar和so文件等等,它们被加载到Android内存中,由一个包转变成可执行的代码,这就需要一 ...

  9. Linux中断管理 (1)Linux中断管理机制

    目录: <Linux中断管理> <Linux中断管理 (1)Linux中断管理机制> <Linux中断管理 (2)软中断和tasklet> <Linux中断管 ...

随机推荐

  1. P4769 [NOI2018]冒泡排序(dp)

    传送门 日常膜拜shadowice巨巨的题解 //minamoto #include<bits/stdc++.h> #define R register #define ll long l ...

  2. sticky,粘性定位

    position:sticky,粘性定位:可以说是relative和fixed的结合: 滑动过程中,元素在显示窗口内则在其本身的位置,超出元素所在位置则显示在设定的sticky位置. 使用: #id ...

  3. Continuous Integration

    https://dzone.com/articles/continuous-delivery-toolchain

  4. VS2015 调试出现无法启动iis express web服务器

    VS2015 调试出现无法启动iis express web服务器 在项目目录下找到.vs文件夹,然后在.vs/config/applicationhost.config找到这个配置文件,删除掉,然后 ...

  5. IDEA | 创建启动SpringBoot项目命令

    clean package spring-boot:run -Dmaven.test.skip=true

  6. CodeForces - 500A-New Year Transportation(模拟)

    New Year is coming in Line World! In this world, there are n cells numbered by integers from 1 to n, ...

  7. linux中c表示字符设备文件符号

    linux中c表示字符设备文件,b表示块设备文件,l表示符号链接文件,r表示可读权限,w表示可写权限.linux文件属性解读:文件类型:-:普通文件 (f)d:目录文件b:块设备文件 (block)c ...

  8. SyntaxHighlighter

    SyntaxHighlighter uses separate syntax files called brushes to define its highlighting functionality ...

  9. 4.词法结构-JavaScript权威指南笔记

    今天是第二章.所谓词法结构(lexical structure),就是写代码中最基本的东西,变量命名,注释,语句分隔等,这是抄书抄的... 1.字符集,必须是Unicode,反正Unicode是ASC ...

  10. android应用开发全程实录-你有多熟悉listview?

    今天给大家带来<android应用开发全程实录>中关于listview和adatper中的部分.包括listview的基本使用,listview的优化等. 我们经常会在应用程序中使用列表的 ...