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上下文对应用进行...

猜你喜欢

Mysql基本使用技巧

 通过几天简单的学习,我掌握了一点基本的mysql操作步骤,下面是我的学习笔记,分享给大家,共同学习。我们都知道数据库在我们开发学习工作中必不可少,mysql作为一种被广泛使用的数据库,所以我们的学习必不可少。 使用mysql我们需要下载安装好所有的工具,在此我不在一一介绍,接下来,我们切入主题,MySQL的简单操作,下面是使用步骤。(我的所有步骤均是在windows下的命令行窗口完成的...

Apache Spark Streaming的优点

Apache Spark Streaming的优点:  (1)优势及特点 1)多范式数据分析管道:能和 Spark 生态系统其他组件融合,实现交互查询和机器学习等多范式组合处理。 2)扩展性:可以运行在 100 个节点以上的集群,延迟可以控制在秒级。 3)容错性:使用 Spark 的 Lineage 及内存维护两份数据进行备份达到容错。 RDD通过 Lineage 记录下之前的操作,如果某节点在运...

论文阅读——MobileNets:Efficient Convolutional Neural Networks for Mobile Vision Applications

本周打算了解一下轻量级网络。先从回顾2017年的MobileNetv1开始。 摘要 MobileNetv1:2017年由Google提出的适用于移动端或嵌入式设备的轻量级网络,首次提出深度可分离卷积(depthwise seperable convolution).
 Section I Introduction 自从AlexNet在2012年ILSVRC比赛中拔得头筹,近年来网络不断朝着更深、更...

C++学习记录之string修改

指定元素的修改 代码: include<iostream> #include<string> using namespace::std; int main(void) { string str; str=("abcdefg"); str[2]='s'; str.at(3)='q'; str.insert(5,"sadfk"); cou...

Zookeeper查看器ZooInspector

一、下载地址 下载ZooInspector https://download.csdn.net/download/u011095110/10790185 解压缩 二、使用方法 已安装JRE/JDK 必须已经安装java运行环境可以运行jar文件 进入命令行模式 进入build/zookeeper-dev-ZooInspector.jar所在的目录 输入zookeeper地址 zookeeperip...

问答精选

Python Regex Matching

I am inputting a text file (Line)(all strings). I am trying to make card_type to be true so it can enter the if statement, however, it never enters the IF statement. The output that comes out from the...

Can't insert this JSON data to SQL Server?

I'm trying to insert the following JSON data into a table on our SQL Server with python code. And if I could solve this with executing a SQL statement, I would be so happy. Because our Application ser...

Spring Instance Factory not working

I started learning spring recently and doing a simple demo of Instance factory but getting error. Java Bean: Interface: Factory: Spring config When I run the code by getting Bike Object from context, ...

How to track direct URL referrer

Most hosts come with softwares or google analytics which allows you to know how a person got to your site, for example: a link on yelp.com or a facebook.com page link. But it is impossible for the sof...

String range search over a RAW datatype

We are using an oracle table as a simple event store, in this table we are saving the message ids (.Net GUID using a SequentialGuidComb) as RAW(16). We are writing a consuming application which needs ...

相关问题

相关文章

热门文章

推荐文章

相关标签

推荐问答