Action<UnityEngine.Object>的使用方法

为了更好理解Action<UnityEngine.Object>,做如下的案例,鼠标点击屏幕一次,实例化出一个cube预设体

这里写图片描述


具体实现方法:


这里写图片描述


这里写图片描述


这里写图片描述


ResManger.cs这个脚本如下:

using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class ResManger : MonoBehaviour {
    public static ResManger instance;

    //之所以设计下面的字典,就是保证一个预设体全局只Load一次
    public Dictionary<string, UnityEngine.Object> zong;
    void Start () {
        zong = new Dictionary<string, UnityEngine.Object>(); //加载的所有预设体
    }


    void Awake()
    {
        instance = this;
    }


    public void shiLiHua(string path, Action<UnityEngine.Object> mm) 
    {
        UnityEngine.Object hh = null;
        if (!zong.ContainsKey(path))
        {
            hh = Resources.Load(path);
            zong.Add(path,hh);

        }
        else
        {
            hh = zong[path];
        }
        GameObject obj= Instantiate(hh) as GameObject;
        mm(obj);
    }
}

xuhaitao.cs这个脚本如下:

using UnityEngine;
using System.Collections;

public class xuhaitao : MonoBehaviour {

    public void  bianSe(object ff)
    {
        GameObject fg = ff as GameObject;
        fg.GetComponent<Renderer>().material.color = Color.red;
    }

    void Update () {
        if (Input.GetMouseButtonDown(0))
        {
            ResManger.instance.shiLiHua("Cube", bianSe);
        }
    }
}

xuhaitao.cs还可以按照下面方式写,最后实现的效果都是一样的

using UnityEngine;
using System.Collections;

public class xuhaitao : MonoBehaviour {

    void Update () {
        if (Input.GetMouseButtonDown(0))
        {
            ResManger.instance.shiLiHua("Cube", (_objj) => {
                GameObject fg = _objj as GameObject;
                fg.GetComponent<Renderer>().material.color = Color.red;
            });
        }
    }
}


值得注意的是: Action的方式也是跨类传值得一种方式


跨类传值另外一种方式:http://blog.youkuaiyun.com/qq_15267341/article/details/72901776


FR:海涛高软(hunk Xu)

Custom animation curve is not configured correctly which may have unexpected results: start value is not 0 or 1. To disable this warning, set 'PrimeTweenConfig.validateCustomCurves = false;'. UnityEngine.Debug:LogWarning (object) PrimeTween.TweenSettings:ValidateCustomCurveKeyframes (UnityEngine.AnimationCurve) (at ./Library/PackageCache/com.kyrylokuzyk.primetween@ef3b05ac79f0/Runtime/TweenSettings.cs:143) PrimeTween.ReusableTween:Setup (object,PrimeTween.TweenSettings&,System.Action`1<PrimeTween.ReusableTween>,System.Func`2<PrimeTween.ReusableTween, PrimeTween.ValueContainer>,bool,PrimeTween.TweenType) (at ./Library/PackageCache/com.kyrylokuzyk.primetween@ef3b05ac79f0/Runtime/Internal/ReusableTween.cs:448) PrimeTween.Tween:animate (object,PrimeTween.TweenSettings`1<UnityEngine.Vector2>&,System.Action`1<PrimeTween.ReusableTween>,System.Func`2<PrimeTween.ReusableTween, PrimeTween.ValueContainer>,PrimeTween.TweenType) (at ./Library/PackageCache/com.kyrylokuzyk.primetween@ef3b05ac79f0/Runtime/Internal/TweenGenerated.cs:2019) PrimeTween.Tween:UIAnchoredPosition (UnityEngine.RectTransform,PrimeTween.TweenSettings`1<UnityEngine.Vector2>) (at ./Library/PackageCache/com.kyrylokuzyk.primetween@ef3b05ac79f0/Runtime/Internal/TweenGenerated.cs:1180) Property_CustomShow:ShowAnimate (bool) (at Assets/Scripts/TweenAnimate/Show/Property_CustomShow.cs:59) ArchivesPanel:PlayPanelAnimate (bool,bool,bool,UnityEngine.Events.UnityAction) (at Assets/Scripts/UIPanel/ArchivesPanel.cs:329) ArchivesPanel:<EventTrigger>b__48_2 () (at Assets/Scripts/UIPanel/ArchivesPanel.cs:234) ArchivesPanel/<>c__DisplayClass51_1:<PlayPanelAnimate>b__3 () (at Assets/Scripts/UIPanel/ArchivesPanel.cs:412) PrimeTween.ReusableTween/<>c:<OnComplete>b__59_0 (PrimeTween.ReusableTween) (at ./Library/PackageCache/com.kyrylokuzyk.primetween@ef3b05ac79f0/Runtime/Internal/ReusableTween.cs:380) PrimeTween.ReusableTween:ReportOnComplete () (at ./Library/PackageCache/com.kyrylokuzyk.primetween@ef3b05ac79f0/Runtime/Internal/ReusableTween.cs:516) PrimeTween.ReusableTween:updateSequence (single,bool,bool) (at ./Library/PackageCache/com.kyrylokuzyk.primetween@ef3b05ac79f0/Runtime/Internal/ReusableTween.cs:188) PrimeTween.ReusableTween:SetElapsedTimeTotal (single,bool) (at ./Library/PackageCache/com.kyrylokuzyk.primetween@ef3b05ac79f0/Runtime/Internal/ReusableTween.cs:104) PrimeTween.ReusableTween:updateAndCheckIfRunning (single) (at ./Library/PackageCache/com.kyrylokuzyk.primetween@ef3b05ac79f0/Runtime/Internal/ReusableTween.cs:79) PrimeTween.PrimeTweenManager:update (System.Collections.Generic.List`1<PrimeTween.ReusableTween>,single,single,int&) (at ./Library/PackageCache/com.kyrylokuzyk.primetween@ef3b05ac79f0/Runtime/Internal/PrimeTweenManager.cs:165) PrimeTween.PrimeTweenManager:Update () (at ./Library/PackageCache/com.kyrylokuzyk.primetween@ef3b05ac79f0/Runtime/Internal/PrimeTweenManager.cs:143)
06-07
出现报错:Command line arguments: -disable-compute-shaders UnityEngine.StackTraceUtility:ExtractStackTrace () Verse.Log:Message (string) Verse.Root:CheckGlobalInit () Verse.Root:Start () Verse.Root_Entry:Start () RimWorld 1.6.4566 rev607 UnityEngine.StackTraceUtility:ExtractStackTrace () Verse.Log:Message (string) RimWorld.VersionControl:LogVersionNumber () Verse.Root:CheckGlobalInit () Verse.Root:Start () Verse.Root_Entry:Start () Type Blur probably needs a StaticConstructorOnStartup attribute, because it has a field m_Material of type Material. All assets must be loaded in the main thread. UnityEngine.StackTraceUtility:ExtractStackTrace () Verse.Log:Warning (string) Verse.StaticConstructorOnStartupUtility/<>c__DisplayClass2_0:<ReportProbablyMissingAttributes>b__0 (System.Type) System.Threading.Tasks.Parallel/<>c__DisplayClass33_0`2<System.Type, object>:<ForEachWorker>b__0 (int) System.Threading.Tasks.Parallel/<>c__DisplayClass19_0`1<object>:<ForWorker>b__1 (System.Threading.Tasks.RangeWorker&,int,bool&) System.Threading.Tasks.TaskReplicator/Replica`1<System.Threading.Tasks.RangeWorker>:ExecuteAction (bool&) System.Threading.Tasks.TaskReplicator/Replica:Execute () System.Threading.Tasks.TaskReplicator/Replica/<>c:<.ctor>b__4_0 (object) System.Threading.Tasks.Task:InnerInvoke () System.Threading.Tasks.Task:Execute () System.Threading.Tasks.Task:ExecutionContextCallback (object) System.Threading.ExecutionContext:RunInternal (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) System.Threading.ExecutionContext:Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) System.Threading.Tasks.Task:ExecuteWithThreadLocal (System.Threading.Tasks.Task&) System.Threading.Tasks.Task:ExecuteEntry (bool) System.Threading.Tasks.Task:System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () System.Threading.ThreadPoolWorkQueue:Dispatch () System.Threading._ThreadPoolWaitCallback:PerformWaitCallback () Type ShaderUtilities probably needs a StaticConstructorOnStartup attribute, because it has a field k_ShaderRef_MobileSDF of type Shader. All assets must be loaded in the main thread. UnityEngine.StackTraceUtility:ExtractStackTrace () Verse.Log:Warning (string) Verse.StaticConstructorOnStartupUtility/<>c__DisplayClass2_0:<ReportProbablyMissingAttributes>b__0 (System.Type) System.Threading.Tasks.Parallel/<>c__DisplayClass33_0`2<System.Type, object>:<ForEachWorker>b__0 (int) System.Threading.Tasks.Parallel/<>c__DisplayClass19_0`1<object>:<ForWorker>b__1 (System.Threading.Tasks.RangeWorker&,int,bool&) System.Threading.Tasks.TaskReplicator/Replica`1<System.Threading.Tasks.RangeWorker>:ExecuteAction (bool&) System.Threading.Tasks.TaskReplicator/Replica:Execute () System.Threading.Tasks.TaskReplicator/Replica/<>c:<.ctor>b__4_0 (object) System.Threading.Tasks.Task:InnerInvoke () System.Threading.Tasks.Task:Execute () System.Threading.Tasks.Task:ExecutionContextCallback (object) System.Threading.ExecutionContext:RunInternal (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) System.Threading.ExecutionContext:Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) System.Threading.Tasks.Task:ExecuteWithThreadLocal (System.Threading.Tasks.Task&) System.Threading.Tasks.Task:ExecuteEntry (bool) System.Threading.Tasks.Task:System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () System.Threading.ThreadPoolWorkQueue:Dispatch () System.Threading._ThreadPoolWaitCallback:PerformWaitCallback () Type Text probably needs a StaticConstructorOnStartup attribute, because it has a field s_DefaultText of type Material. All assets must be loaded in the main thread. UnityEngine.StackTraceUtility:ExtractStackTrace () Verse.Log:Warning (string) Verse.StaticConstructorOnStartupUtility/<>c__DisplayClass2_0:<ReportProbablyMissingAttributes>b__0 (System.Type) System.Threading.Tasks.Parallel/<>c__DisplayClass33_0`2<System.Type, object>:<ForEachWorker>b__0 (int) System.Threading.Tasks.Parallel/<>c__DisplayClass19_0`1<object>:<ForWorker>b__1 (System.Threading.Tasks.RangeWorker&,int,bool&) System.Threading.Tasks.TaskReplicator/Replica`1<System.Threading.Tasks.RangeWorker>:ExecuteAction (bool&) System.Threading.Tasks.TaskReplicator/Replica:Execute () System.Threading.Tasks.TaskReplicator/Replica/<>c:<.ctor>b__4_0 (object) System.Threading.Tasks.Task:InnerInvoke () System.Threading.Tasks.Task:Execute () System.Threading.Tasks.Task:ExecutionContextCallback (object) System.Threading.ExecutionContext:RunInternal (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) System.Threading.ExecutionContext:Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) System.Threading.Tasks.Task:ExecuteWithThreadLocal (System.Threading.Tasks.Task&) System.Threading.Tasks.Task:ExecuteEntry (bool) System.Threading.Tasks.Task:System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () System.Threading.ThreadPoolWorkQueue:Dispatch () System.Threading._ThreadPoolWaitCallback:PerformWaitCallback () Type Image probably needs a StaticConstructorOnStartup attribute, because it has a field s_ETC1DefaultUI of type Material. All assets must be loaded in the main thread. UnityEngine.StackTraceUtility:ExtractStackTrace () Verse.Log:Warning (string) Verse.StaticConstructorOnStartupUtility/<>c__DisplayClass2_0:<ReportProbablyMissingAttributes>b__0 (System.Type) System.Threading.Tasks.Parallel/<>c__DisplayClass33_0`2<System.Type, object>:<ForEachWorker>b__0 (int) System.Threading.Tasks.Parallel/<>c__DisplayClass19_0`1<object>:<ForWorker>b__1 (System.Threading.Tasks.RangeWorker&,int,bool&) System.Threading.Tasks.TaskReplicator/Replica`1<System.Threading.Tasks.RangeWorker>:ExecuteAction (bool&) System.Threading.Tasks.TaskReplicator/Replica:Execute () System.Threading.Tasks.TaskReplicator/Replica/<>c:<.ctor>b__4_0 (object) System.Threading.Tasks.Task:InnerInvoke () System.Threading.Tasks.Task:Execute () System.Threading.Tasks.Task:ExecutionContextCallback (object) System.Threading.ExecutionContext:RunInternal (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) System.Threading.ExecutionContext:Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) System.Threading.Tasks.Task:ExecuteWithThreadLocal (System.Threading.Tasks.Task&) System.Threading.Tasks.Task:ExecuteEntry (bool) System.Threading.Tasks.Task:System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () System.Threading.ThreadPoolWorkQueue:Dispatch () System.Threading._ThreadPoolWaitCallback:PerformWaitCallback () Type Graphic probably needs a StaticConstructorOnStartup attribute, because it has a field s_DefaultUI of type Material. All assets must be loaded in the main thread. UnityEngine.StackTraceUtility:ExtractStackTrace () Verse.Log:Warning (string) Verse.StaticConstructorOnStartupUtility/<>c__DisplayClass2_0:<ReportProbablyMissingAttributes>b__0 (System.Type) System.Threading.Tasks.Parallel/<>c__DisplayClass33_0`2<System.Type, object>:<ForEachWorker>b__0 (int) System.Threading.Tasks.Parallel/<>c__DisplayClass19_0`1<object>:<ForWorker>b__1 (System.Threading.Tasks.RangeWorker&,int,bool&) System.Threading.Tasks.TaskReplicator/Replica`1<System.Threading.Tasks.RangeWorker>:ExecuteAction (bool&) System.Threading.Tasks.TaskReplicator/Replica:Execute () System.Threading.Tasks.TaskReplicator/Replica/<>c:<.ctor>b__4_0 (object) System.Threading.Tasks.Task:InnerInvoke () System.Threading.Tasks.Task:Execute () System.Threading.Tasks.Task:ExecutionContextCallback (object) System.Threading.ExecutionContext:RunInternal (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) System.Threading.ExecutionContext:Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) System.Threading.Tasks.Task:ExecuteWithThreadLocal (System.Threading.Tasks.Task&) System.Threading.Tasks.Task:ExecuteEntry (bool) System.Threading.Tasks.Task:System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () System.Threading.ThreadPoolWorkQueue:Dispatch () System.Threading._ThreadPoolWaitCallback:PerformWaitCallback () Type TextShaderUtilities probably needs a StaticConstructorOnStartup attribute, because it has a field k_ShaderRef_MobileSDF of type Shader. All assets must be loaded in the main thread. UnityEngine.StackTraceUtility:ExtractStackTrace () Verse.Log:Warning (string) Verse.StaticConstructorOnStartupUtility/<>c__DisplayClass2_0:<ReportProbablyMissingAttributes>b__0 (System.Type) System.Threading.Tasks.Parallel/<>c__DisplayClass33_0`2<System.Type, object>:<ForEachWorker>b__0 (int) System.Threading.Tasks.Parallel/<>c__DisplayClass19_0`1<object>:<ForWorker>b__1 (System.Threading.Tasks.RangeWorker&,int,bool&) System.Threading.Tasks.TaskReplicator/Replica`1<System.Threading.Tasks.RangeWorker>:ExecuteAction (bool&) System.Threading.Tasks.TaskReplicator/Replica:Execute () System.Threading.Tasks.TaskReplicator/Replica/<>c:<.ctor>b__4_0 (object) System.Threading.Tasks.Task:InnerInvoke () System.Threading.Tasks.Task:Execute () System.Threading.Tasks.Task:ExecutionContextCallback (object) System.Threading.ExecutionContext:RunInternal (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) System.Threading.ExecutionContext:Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) System.Threading.Tasks.Task:ExecuteWithThreadLocal (System.Threading.Tasks.Task&) System.Threading.Tasks.Task:ExecuteEntry (bool) System.Threading.Tasks.Task:System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () System.Threading.ThreadPoolWorkQueue:Dispatch () System.Threading._ThreadPoolWaitCallback:PerformWaitCallback ()
最新发布
09-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值