Unity关于不同脚本之间相互调用变量的方法

转自 https://blog.csdn.net/jennyhigh/article/details/80900538
本来是想绕开一个脚本调用其他脚本变量这个事情的,但是不管怎么写最后还是绕回来了,有些功能必须这样才好完成。

小伙伴们大同小异地提供了三种方法,但都是与加载组件有关系,比如这个:
在这里插入图片描述
原文地址:https://blog.csdn.net/woerxi/article/details/17621841#commentBox

但是我不喜欢,只是调用脚本的变量,还有把变量他妈和他奶奶一起带上,好麻烦……

至少我这个功能不需要这样,除非有其他的顺带功能。

以前我也写过脚本互调变量的代码,只是忘了,所以我想肯定有其他的办法,终于在蛮牛上找到了,其中一个小伙伴是这样写的:

在这里插入图片描述
原来就这么简单,所以最后我的代码是这样的:

public class PickBadgeA : MonoBehaviour {

public static int testcount;    //定义一个静态共有;

if (pick) 
{
                       ……
testcount = 1;
Debug.Log ("捡起来了!");

}

……………………脚本分割线……………………

public class Addtime : MonoBehaviour {


public int addcount;

void Update ()
{
……
if (check)
{

addcount = PickBadgeA.testcount + PickBadgeB.testcountB+OpenCabinet.testcountC;

                       //调用上个脚本的静态共有;

}

无意中还找到一个调用函数的,感觉也不错,地址如下:
https://blog.csdn.net/liumou111/article/details/46754051
----------------分割线-----------------
调用其他实例的脚本是一种很常用的需求,在Unity3D中实现起来也有多种方法,假设在ScriptB有一个DoSomething函数,如下所示:

using UnityEngine;

public class ScriptB : MonoBehaviour {

public void DoSomething() {

Debug.Log("Hi there");

}

}

在ScriptA中如果要访问某个GameObject的ScriptB脚本中的DoSomething函数:

using UnityEngine;

public class ScriptA : MonoBehaviour {

public ScriptB other;

void Update() {

other.DoSomething();

}

}

注意ScriptA有一个ScriptB类型的公共变量,这是一个绝妙的小技巧,如此一来你便可以通过编辑器拖拽任何含有ScriptB这一脚本的GameObject至该变量上(因为是public的,所以暴露于编辑器界面中),Unity会自动识别你需要赋值的不是那个GameObject,而是它所包含的ScriptB脚本。

除此之外,如果你不喜欢这种在编辑器中直接赋值的办法,你可以通过GameObject.Find(“somename”) 来获取GameObject:

GameObject go = GameObject.Find(“somegameobjectname”);

ScriptB other = (ScriptB) go.GetComponent(typeof(ScriptB));

other.DoSomething();

不过需要注意的是GameObject.Find()的执行效率非常低,所以如果可能的话还是尽可能使用“直接赋值法”吧。

来源:网络


智能推荐

类与对象,方法的相互调用

1、类与对象的关系:         类是对象的抽象,而对象是类的具体实例。类是抽象的,而对象是具体的。         类是一组有相同属性和方法的对象的集合。 2、类的定义:      &...

Unity与Android相互调用-AndroidStudio调用Unity的代码

Unity与Android相互调用-配置AndroidStudio工程 Unity与Android相互调用-导出Aar包和Unity调用AndroidStudio的代码 1.修改unity端的代码,增加修改颜色的函数,供AandroidStudio调用。 2.打开AandroidStudio工程 在MainActivity中增加箭头指示的代码,第一个参数代表要调用的unity脚本在哪个物体身上,第...

Unity与Android相互调用-导出Aar包和Unity调用AndroidStudio的代码

Unity与Android相互调用-配置AndroidStudio工程 Unity与Android相互调用-AandroidStudio调用Unity的代码 1.打开下图箭头所指向的文件 修改后的代码下图所示,增加一个加法函数。 2.导出Aar包供Unity使用 选中testLibrary,点击Build->Make Module'testLibrary' 等待导出完成。 3.如下两张图所示...

Unity与Android方法相互调用一(使用aar包扩展ManiActivity)

Unity与Android交互 Unity和Android交互方法主要有两种, 1.导出aar包供unity使用,这种是Unity官方推荐使用的,也是这篇文章主要讲解的。 2.导出jar包供unity使用。 本片讲第一种,使用aar包。 首先,使用AndroidStudio新建一个空工程,选项都用默认即可。 打开工程后,File->New->New Module,选中Android L...

Unity与Android方法相互调用二(使用jar包提供扩展类)

使用jar包进行Unity与Android交互分两种方式。 一种是通过扩展MainActivity。 另一种是提供扩展类。 第一种扩展MainActivity方式作用和第一篇讲的使用arr包基本相同,且这种方式Unity官方已不推荐使用,以后可能会逐渐废置。 本篇主要讲第二种方式,提供扩展类。这种方式不需要导出AndroidMini。但相对的也有局限性,比如不能通过Activity上下文对应用进行...

猜你喜欢

二叉树(C++模板实现)

一、实验要求    <1>打印出二叉树的三种遍历序。     <2>设计算法按中序次序输出二叉树中各结点的值及其所对应的层次数。     <3>求二叉树的高度。     <4>求二叉树的结点数。     <5>...

语音识别学习记录 [TDNN时延神经网络]

最近了解了卷积神经网络(CNN),CNN是受语音信号处理中时延神经网络(TDNN)影响而发明的。本篇的大部分内容都来自关于TDNN原始文献Waibel A, Hanazawa T, Hinton G, et al. Phoneme recognition using time-delay neural networks[J] (TDNN的原始论文)的理解和整理。该文写与1989年,在识别"...

教你快速推广微信公总号

随着互联网科技的飞速发展,自媒体平台正在发挥着越来越大的影响力,企业进行互联网的品牌建设,一定会通过网络媒体,推广自己在网络中的知名度以及品牌内容,从而形成公众认识的企业品牌形象。同事个人自媒体也在发光发热,多少人在自媒体这个行业里赚到了人生的第一桶金。公总号作为自媒体的代表,自然也吸引了不少人的加入,导致现在的微信公总号也是越来越难做了。现在微信公总号的粉丝价格已经涨到了2-3元一个粉丝的地步。...

VMware+ubuntu安装以及使用问题总结 (蓝屏,屏幕小,无法操作鼠标)

前言 需要使用VMware来创建ubuntu虚拟机。 安装VMware 我是参考伪万能的洋桑 安装的VMware。 但是一定要去官网去下载最新的VMware,如果使用旧版的在打开创建好的虚拟机会出现蓝屏问题。 关于**问题,直接搜最新的***即可。 安装ubuntu 参考悠悠happy安装ubuntu。 这里需要下载iso文件。几个G的那种,不要下载错了。 这里想要图形化界面,下载ubuntu-1...

老外写的一个测试用例

Test case example:   Test Case ID: CUST.01   Function: Add a new Customer   Data Assumptions: Customer database has been restored   General Description:   Add a new customer, via the Customer Add...

问答精选

How to get the hostname in Google App Engine?

I'm developing an app with Python on Google App Engine at http://shs1509-grc.appspot.com/ And since I am in China, appspot is blocked, and my users have to use the hostname http://shs1509-grc.appsp0t....

SessionNotCreatedException in Eclipse "Appium" code

My Code to execute a simple click button automation is given below , I am very new to appium so I am just trying to get my app and click one button , as i have given my exception down it is stopping m...

Get the count of checkboxes checked in a List using Javascript

I have a multiple Input Checkbox List and I want to create a JS function that when the user selects more than specified count items in the checkbox list, it will reject the action but if they have few...

Deleting index from Solr using solrj as a client

I am using solrj as client for indexing documents on the solr server. I am having problem while deleting the indexes by 'id' from the solr server. I am using following code to delete the indexes: Afte...

BATCH Scripting - Swapping array subscript values

I'd like to start this off by saying batch scripting is something I ever do, and it's for an assignment in my class, so please bear with me. I am trying to take an array index and swap one of the indi...

相关问题

相关文章

热门文章

推荐文章

相关标签

推荐问答