以编程方式定义脚本的执行顺序 [英] Programmatically define execution order of scripts

查看:97
本文介绍了以编程方式定义脚本的执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过以编程方式将脚本添加到给定的游戏对象中,这些脚本会按添加顺序执行吗?他们的事件会按照添加的顺序运行吗?

By programmatically adding scripts to a given game object, will these scripts execute in the order they were added? Will their events run in the order they were added?

void Awake ()
{
    gameObject.AddComponent("Script_1");
    gameObject.AddComponent("Script_2");
}

推荐答案

简短答案:否.但是您可以设置设置中的脚本执行顺序(菜单:编辑">项目设置">脚本执行顺序"),或从代码中更改它:

Short answer: No. But you can set Script Execution Order in the settings (menu: Edit > Project Settings > Script Execution Order) or change it from code:

// First you get the MonoScript of your MonoBehaviour
MonoScript monoScript = MonoScript.FromMonoBehaviour(yourMonoBehaviour);

// Getting the current execution order of that MonoScript
int currentExecutionOrder = MonoImporter.GetExecutionOrder(monoScript);

// Changing the MonoScript's execution order
MonoImporter.SetExecutionOrder(monoScript, x);

1)脚本执行顺序操作

2)从代码更改Unity脚本的执行顺序

3)动态更改脚本的执行顺序(来自脚本)

最近我也遇到了类似的问题,发现这个问题没有答案.因此决定发布有用的信息,希望对您有所帮助:)

Recently I've also faced similar issue, and found this question without an answer. Thus decided to post useful information, hope it helps :)

这篇关于以编程方式定义脚本的执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆