【发布时间】:2018-06-22 05:21:42
【问题描述】:
我是 Unity 的新手,我在我的 Assets 文件夹中制作了一些脚本。我可以很好地将它们附加到物体上并且它们可以工作。但是,如果我将相同的脚本附加到多个对象,游戏就会中断,并且对一个对象上的脚本所做的更改会影响另一个同名对象上的脚本。在将它们应用于不同的对象之前,我是否必须复制每个脚本以防止这种情况发生?每个对象都需要有相同的脚本,但如果有意义的话,脚本需要是独立的。
我尝试对有类似问题的人进行研究,然后出现了继承的话题。但这意味着我仍然必须为每个对象编写一个子类。我想只写一次,然后将其应用于所有对象,然后能够修改每个具有不同属性的对象上的每个脚本
谢谢
编辑:更具体
我的问题确实与未在对象上触发的动画触发器有关。我有 2 个对象,终结者和 LieutenantDan。这两个模型都有动画,并且动画在 LieutenantDan 上无法正常工作,尽管它们在终结者上运行良好。两个模型都附有相同的 3 个脚本:
BasicProperties.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// PURPOSE: Defines basic root properties for many object types
// ATTACHMENT: Many types of objects
public class BasicProperties : MonoBehaviour {
public int objectType;
public string animWalkTrigger;
public int facingInversion;
void Start()
{
}
void Update()
{
}
}
Champion Properties.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ATTACHMENT: Game Play Objects
// this Class initializes custom properties of the champion that can be
manipulated
// as the game progresses
public class ChampionProperties : MonoBehaviour {
// whether this object can be selected or not
public bool selectable;
// whether the user has currently selected the champion or not.
public bool selected;
// whether the object is moveable
public bool moveable;
// if the object is moveable it has to have a movespeed.
public float moveSpeed;
// Use this for initialization
void Start () {
// the champion is selectable
selectable = true;
// on load the champion is not selected
selected = false;
// the champion is moveable
moveable = true;
// set the moveSpeed
moveSpeed = 10f;
}
void Update () {
}
}
ChampionBasicActions.cs
public class ChampionBasicActions : MonoBehaviour {
public ChampionProperties championProperties;
public BasicProperties basicProperties;
static Animator anim;
public Vector3 objPos;
public Vector3 destPos;
void Start () {
basicProperties = this.GetComponent<BasicProperties>();
championProperties = this.GetComponent<ChampionProperties>();
anim = GetComponent<Animator>();
objPos = this.transform.position;
destPos = Vector3.zero;
}
void Update () {
if ( destPos != Vector3.zero )
{
anim.SetBool(basicProperties.animWalkTrigger, true);
float fpsMoveSpeed = championProperties.moveSpeed * Time.deltaTime;
float maintainY = transform.position.y;
destPos.y = maintainY;
transform.position = Vector3.MoveTowards(this.transform.position, destPos, fpsMoveSpeed);
if ( Vector3.Distance(this.transform.position, destPos) < 0.01 )
{
this.transform.position = destPos;
destPos = Vector3.zero;
} else {
Vector3 direction = (destPos - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(basicProperties.facingInversion * direction);
transform.rotation = Quaternion.SlerpUnclamped(transform.rotation, lookRotation, Time.deltaTime * 100f);
}
} else {
anim.SetBool(basicProperties.animWalkTrigger, false);
}
}
}
对于每个模型的基本属性脚本,在 Inspector 视图中,我已将“AnimWalkTrigger”变量定义为 LieutenantDan 的“isWalkingLtDan”和终结者的“isWalkingTerm”。
两个模型都以空闲动画开始,但有一个行走动画,每个模型都由不同的布尔触发器定义。对于 LieutenantDan,开始/停止步行的布尔触发器是“isWalkingLtDan”,而对于终结者,步行布尔触发器是“isWalkingTerm”
当我播放场景时,两个角色都按预期处于空闲位置。我可以点击终结者,让他四处移动,他按预期移动,然后按预期返回空闲位置。
但是,对于丹中尉,我可以点击他并移动他,但他从不进入行走动画,他只是保持空闲状态。随着场景的播放,我在控制台中收到此错误消息
Parameter 'isWalkingLtDan' does not exist
这是指
anim.SetBool(basicProperties.animWalkTrigger, false);
调用附加到每个对象的 ChampionBasicActions 脚本。在附加到每个对象的 BasicProperties 脚本中,我在 Inspector 视图中将变量 animWalkTrigger 定义为 Terminator 上的“isWalkingTerm”和 Lieutenant Dan 上的“isWalkingLtDan”。
对于终结者来说,Animator中有一个从Idle到Walking再回到Idle的过渡,触发器是参数'isWalkingTerm'。这似乎设置正确。
对于 LieutenantDan,动画之间存在相同的过渡,但触发器正确设置为“isWalkingLtDan”。
那么到底为什么我会收到此错误消息?如果我停用 Terminator 上与 LieutenantDan 共享的 3 个脚本,LieutenantDan 突然工作正常并按预期行走,并且控制台中的错误消息停止。怎么回事?
谢谢
【问题讨论】:
-
引用相同脚本的对象将获得相同的功能。属性/字段可以更改某些行为,前提是您将其写入脚本。对象上没有脚本副本,它们有对脚本的引用。更改脚本会更改引用它的每个对象。
-
你能提供一些代码吗?