UNITY中有Timer
using UnityEngine;
using System.Collections;
using System.Timers; public class NewBehaviourScript : MonoBehaviour { // Use this for initialization
void Start () { Timer t = new Timer(100);
t.Elapsed += T_Elapsed_Handle;
t.Start(); } private void T_Elapsed_Handle(object sender, ElapsedEventArgs e)
{
Debug.Log("T_Elapsed_Handle" + this.gameObject.name);
}
输出:
get_gameObject can only be called from the main thread.
看来Coroutin的出现就是为了解决这个问题,方便大家使用,
当然往主线程里Enqeue消息也是可以的
百度了一下,找到了Loom这个插件。感觉真的很好用,错误不见了。
这里做下记录。
Loom插件就一个脚本导入到Unity中就行了。具体脚本内容如下
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Threading;
using System.Linq; public class Loom :MonoBehaviour
{
public static int maxThreads = 8;
static int numThreads; private static Loom _current;
//private int _count;
public static Loom Current
{
get
{
Initialize();
return _current;
}
} void Awake()
{
_current = this;
initialized = true;
} static bool initialized; public static void Initialize()
{
if (!initialized)
{ if (!Application.isPlaying)
return;
initialized = true;
var g = new GameObject("Loom");
_current = g.AddComponent<Loom>();
#if !ARTIST_BUILD
UnityEngine.Object.DontDestroyOnLoad(g);
#endif
} }
public struct NoDelayedQueueItem
{
public Action<object> action;
public object param;
} private List<NoDelayedQueueItem> _actions = new List<NoDelayedQueueItem>();
public struct DelayedQueueItem
{
public float time;
public Action<object> action;
public object param;
}
private List<DelayedQueueItem> _delayed = new List<DelayedQueueItem>(); List<DelayedQueueItem> _currentDelayed = new List<DelayedQueueItem>(); public static void QueueOnMainThread(Action<object> taction, object tparam)
{
QueueOnMainThread(taction, tparam, 0f);
}
public static void QueueOnMainThread(Action<object> taction, object tparam, float time)
{
if (time != 0)
{
lock (Current._delayed)
{
Current._delayed.Add(new DelayedQueueItem { time = Time.time + time, action = taction, param = tparam });
}
}
else
{
lock (Current._actions)
{
Current._actions.Add(new NoDelayedQueueItem { action = taction, param = tparam });
}
}
} public static Thread RunAsync(Action a)
{
Initialize();
while (numThreads >= maxThreads)
{
Thread.Sleep(100);
}
Interlocked.Increment(ref numThreads);
ThreadPool.QueueUserWorkItem(RunAction, a);
return null;
} private static void RunAction(object action)
{
try
{
((Action)action)();
}
catch
{
}
finally
{
Interlocked.Decrement(ref numThreads);
} } void OnDisable()
{
if (_current == this)
{ _current = null;
}
} // Use this for initialization
void Start()
{ } List<NoDelayedQueueItem> _currentActions = new List<NoDelayedQueueItem>(); // Update is called once per frame
void Update()
{
if (_actions.Count > 0)
{
lock (_actions)
{
_currentActions.Clear();
_currentActions.AddRange(_actions);
_actions.Clear();
}
for (int i = 0; i < _currentActions.Count; i++)
{
_currentActions[i].action(_currentActions[i].param);
}
} if (_delayed.Count > 0)
{
lock (_delayed)
{
_currentDelayed.Clear();
_currentDelayed.AddRange(_delayed.Where(d => d.time <= Time.time));
for (int i = 0; i < _currentDelayed.Count; i++)
{
_delayed.Remove(_currentDelayed[i]);
}
} for (int i = 0; i < _currentDelayed.Count; i++)
{
_currentDelayed[i].action(_currentDelayed[i].param);
}
}
}
}
代码也就100多行,主要是两个比较主要的方法
RunAsync(Action a)和QueueOnMainThread(Action<object> taction, object tparam)
开启一个线程然后在Loom.RunAsyn()中调用需要回到Unity主线程更新界面时调用QueueOnMainThread()即可。简单好用。
UNITY中有Timer的更多相关文章
- Unity中有两种Animation Clip
http://blog.csdn.net/zzxiang1985/article/details/51291861 在Unity中,我们有两种方法创建Animation Clip. 一种(后面简称方法 ...
- Unity C#最佳实践(上)
本文为<effective c#>的读书笔记,此书类似于大名鼎鼎的<effective c++>,是入门后提高水平的进阶读物,此书提出了50个改进c#代码的原则,但是由于主要针 ...
- Unity性能优化(4)-官方教程Optimizing graphics rendering in Unity games翻译
本文是Unity官方教程,性能优化系列的第四篇<Optimizing graphics rendering in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...
- EJB之Timer
EJB Timer 要么: Annotation @Schedule 或者方法前声明@Timeout 要么: 在部署描述中定义timeout-method 如果是使用@Schedule, Timer在 ...
- [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之灯光介绍Lights
[我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之灯光介绍Lights 既上一篇分享了中文字幕的摄像机介绍Cameras后,本篇分享一下第2个已完工的 ...
- Unity教程之再谈Unity中的优化技术
这是从 Unity教程之再谈Unity中的优化技术 这篇文章里提取出来的一部分,这篇文章让我学到了挺多可能我应该知道却还没知道的知识,写的挺好的 优化几何体 这一步主要是为了针对性能瓶颈中的”顶点 ...
- 【Unity技巧】Unity中的优化技术
http://blog.csdn.net/candycat1992/article/details/42127811 写在前面 这一篇是在Digital Tutors的一个系列教程的基础上总结扩展而得 ...
- Unity目录结构
http://www.cnblogs.com/liudq/p/5540051.htmlUnity中有几个默认目录 Unity5.x Resources 项目中默认的资源路径,会直接打包到游戏包中.即使 ...
- Unity 4.3 2D 教程:新手上路
这篇文章译自 Christopher LaPollo 先生的 Unity 4.3 2D 教程的第一部分 Unity 4.3 2D Tutorial: Getting Started 感谢这套优秀教程的 ...
随机推荐
- 网站使用 rel="noopener" 打开外部锚
当您的页面链接至使用 target="_blank" 的另一个页面时,新页面将与您的页面在同一个进程上运行. 如果新页面正在执行开销极大的 JavaScript,您的页面性能可能会 ...
- Mac Book Pro重新安装出错
错误描述 未能创建用于apfs安装的预启动宗卷 解决 网上的经验: 返场重修 多试几次拼人品 多试了几次之后还是没用,选择U盘安装. 搞定! U盘安装教程
- BZOJ4886: [Lydsy1705月赛]叠塔游戏(环套树森林&贪心)
4886: [Lydsy1705月赛]叠塔游戏 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 198 Solved: 76[Submit][Stat ...
- [团队项目]Scrum 项目1.0 (演说视频)
1.确定选题. 应用NABCD模型,分析你们初步选定的项目,充分说明你们选题的理由. 录制为演说视频,上传到视频网站,并把链接发到团队博客上. 截止日期:2016.5.6日晚10点 2.SCRUM 流 ...
- LG3389 【模板】高斯消元法
题意 题目描述 给定一个线性方程组,对其求解 输入输出格式 输入格式: 第一行,一个正整数\(n\) 第二至\(n+1\)行,每行\(n+1\)个整数,为\(a_1, a_2 \cdots a_n\) ...
- 解决Mybatis没有代码提示
MyBatis xml文件中代码自动提示 工具/原料 eclipse,maven 方法/步骤 1 一.获得mybatis-3-config.dtd.mybatis-3-mapper.dtd 这 ...
- 整理了一下 ThinkPHP 历史
整理了一下 ThinkPHP 历史 ThinkPHP 一款国内最流行的 PHP 开源框架.
- 【转】shell 编程:冒号 后面跟 等号,加号,减号,问号的意义
原文网址:http://blog.csdn.net/trochiluses/article/details/9048539 缺省值(:-) 如果变量后面跟着冒号和减号,则变量后面跟着是这个变量的缺 ...
- Spring Boot 入门之消息中间件篇(五)
原文地址:Spring Boot 入门之消息中间件篇(五) 博客地址:http://www.extlight.com 一.前言 在消息中间件中有 2 个重要的概念:消息代理和目的地.当消息发送者发送消 ...
- 去掉桌面SVN
1新建记事本 2for /r . %%a in (.) do @if exist "%%a\.svn" rd /s /q "%%a\.svn" 3重命名 删除S ...