[英]UNity3D, return value from one class/method to another class/method in Unity3D
In Unity3D, 在Unity3D中,
I have a weapon script and a stamina script. 我有一个武器脚本和一个耐力脚本。 What I want to do now is to drain stamina when my weapon swings. 我现在想做的是在武器摆动时消耗耐力。
I tried my code and i have been playing around with it for a couple of hours. 我尝试了我的代码,并且已经使用了几个小时。
I am using unity so it also gives me a comment about using new and that I should use Add.component etc so I would appreciate an answer to that question as well! 我正在使用unity,因此它也给了我关于使用new的注释,并且我应该使用Add.component等,因此我也很乐意回答该问题!
Hopefully this post is a bit better in terms of the title and information/layout as I am very tired and low on energy. 希望这篇文章在标题和信息/布局方面会更好一些,因为我非常疲倦且精力充沛。 Im going to take a short food break before I get back to it! 我要休息片刻,然后再回来!
Here is the Stamina system : 这是耐力系统 :
` `
public class HandS : MonoBehaviour {
public int startingHealth = 100;
public int currentHealth;
public int healthReg;
Sword mySword;
bool isRegenHealth;
public float startingStam = 100;
public float currentStam;
public float stamReg;
bool isRegenStam;
void Awake()
{
currentStam = startingStam;
}
void Update()
{
if (currentStam != startingStam && !isRegenStam)
{
StartCoroutine(RegainStamOverTime());
}
}
private IEnumerator RegainStamOverTime()
{
isRegenStam = true;
while (currentStam < startingStam)
{
Stamregen();
ReduceStamina(mySword.stamDrain);
yield return new WaitForSeconds(1);
}
isRegenStam = false;
}
public void Stamregen()
{
currentStam += stamReg;
}
public void ReduceStamina(float _stamDrain)
{
currentStam -= _stamDrain;
}
} }
` `
Here is the Sword script : 这是Sword脚本 :
using UnityEngine;
using System.Collections;
public class Sword : MonoBehaviour {
static Animator anim;
public GameObject hitbox;
HandS hp = new HandS();
public int Sworddamage = 20;
public float sec = 0.5f;
public float maxStamina = 20;
public float AttackCD;
public float delayBetweenAttacks = 1.5f;
public float stamDrain = 50;
public AudioSource WeaponSource;
public AudioClip WeaponSound;
void Start () {
anim = GetComponentInParent<Animator>();
WeaponSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update () {
attack();
block();
} }
public void attack()
{
if (Input.GetButtonDown("Fire1") && Time.time > AttackCD)
{
AttackCD = Time.time + delayBetweenAttacks;
anim.SetBool("IsAttacking", true);
hitbox.SetActive(true);
StartCoroutine(LateCall());
WeaponSource.PlayOneShot(WeaponSound);
Debug.Log("hit");
hp.ReduceStamina(stamDrain);
}
else
{
anim.SetBool("IsAttacking", false);
}
} }
public void block()
{
if (Input.GetButtonDown("Fire2"))
{
anim.SetBool("IsBlocking", true);
}
else
{
anim.SetBool("IsBlocking", false);
}
}
IEnumerator LateCall()
{
yield return new WaitForSeconds(sec);
hitbox.SetActive(false);
}
} }
You can create getter/setter methods to set/get attributes from the Class A. In your class B, you can create an instance o class A and access to that attibutes. 您可以创建getter / setter方法来设置/获取类A的属性。在类B中,您可以创建类A的实例并访问该服装。
Example Class A: 示例类A:
public class Genre
{
public string Name { get; set; }
}
Example instance in Class B: B类的实例实例:
Genre genre = new Genre();
string name = genre.getName();
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.