c# - 如何从另一个脚本访问脚本中的枚举?
问题描述
带有 enum 的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InteractableItem : MonoBehaviour
{
public enum InteractableMode // your custom enumeration
{
Description,
Action
};
public InteractableMode interactableMode = InteractableMode.Description;
public float distance;
private bool action = true;
[TextArea(1, 10)]
public string description = "";
public void ActionOnItem()
{
if(interactableMode == InteractableMode.Action && distance <= 5f && action == true)
{
action = false;
}
}
}
以及我试图从中访问的脚本:
using UnityEngine;
using System;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
[RequireComponent(typeof(Animator))]
public class IKControl : MonoBehaviour
{
public InteractableItem[] lookObj = null;
public GameObject objToThrow;
public Text text;
public Text textMultiLine;
public float weightDamping = 1.5f;
public float maxDistance = 10f;
public bool RightHandToTarget = true;
public float throwSpeed;
private List<InteractableItem> allDetectedItems;
private Animator animator;
private InteractableItem lastPrimaryTarget;
private Quaternion savedRotation;
private float lerpEndDistance = 0.1f;
private float finalLookWeight = 0;
private bool transitionToNextTarget = false;
private InteractableItem target;
private bool throwObj = false;
void Start()
{
animator = GetComponent<Animator>();
allDetectedItems = new List<InteractableItem>();
}
// Callback for calculating IK
void OnAnimatorIK()
{
if (lookObj != null)
{
InteractableItem primaryTarget = null;
float closestLookWeight = 0;
// Here we find the target which is closest (by angle) to the players view line
allDetectedItems.Clear();
foreach (InteractableItem target in lookObj)
{
Vector3 lookAt = target.transform.position - transform.position;
lookAt.y = 0f;
// Filter out all objects that are too far away
//if (lookAt.magnitude > maxDistance) continue;
if (lookAt.magnitude > target.distance) continue;
float dotProduct = Vector3.Dot(new Vector3(transform.forward.x, 0f, transform.forward.z).normalized, lookAt.normalized);
float lookWeight = Mathf.Clamp(dotProduct, 0f, 1f);
if (lookWeight > 0.1f && lookWeight > closestLookWeight)
{
closestLookWeight = lookWeight;
primaryTarget = target;
allDetectedItems.Add(target);
}
}
if (primaryTarget != null)
{
if ((lastPrimaryTarget != null) && (lastPrimaryTarget != primaryTarget) && (finalLookWeight > 0f))
{
// Here we start a new transition because the player looks already to a target but
// we have found another target the player should look at
transitionToNextTarget = true;
}
}
// The player is in a neutral look position but has found a new target
if ((primaryTarget != null) && !transitionToNextTarget)
{
lastPrimaryTarget = primaryTarget;
//finalLookWeight = Mathf.Lerp(finalLookWeight, closestLookWeight, Time.deltaTime * weightDamping);
finalLookWeight = Mathf.Lerp(finalLookWeight, 1f, Time.deltaTime * weightDamping);
float bodyWeight = finalLookWeight * .75f;
animator.SetLookAtWeight(finalLookWeight, bodyWeight, 1f);
animator.SetLookAtPosition(primaryTarget.transform.position);
if (RightHandToTarget)
{
Vector3 relativePos = primaryTarget.transform.position - transform.position;
Quaternion rotationtoTarget = Quaternion.LookRotation(relativePos, Vector3.up);
animator.SetIKRotationWeight(AvatarIKGoal.RightHand, finalLookWeight);
animator.SetIKRotation(AvatarIKGoal.RightHand, rotationtoTarget);
animator.SetIKPositionWeight(AvatarIKGoal.RightHand, finalLookWeight * 1f * closestLookWeight);
animator.SetIKPosition(AvatarIKGoal.RightHand, primaryTarget.transform.position);
// -> new code block
if (finalLookWeight > 0.95f) // here you can play with a value between 0.95f -> 1.0f
{
// call your funtion to shoot something here
throwObj = true;
target = primaryTarget;
}
}
}
// Let the player smoothly look away from the last target to the neutral look position
if ((primaryTarget == null && lastPrimaryTarget != null) || transitionToNextTarget)
{
finalLookWeight = Mathf.Lerp(finalLookWeight, 0f, Time.deltaTime * weightDamping);
float bodyWeight = finalLookWeight * .75f;
animator.SetLookAtWeight(finalLookWeight, bodyWeight, 1f);
animator.SetLookAtPosition(lastPrimaryTarget.transform.position);
if (RightHandToTarget)
{
Vector3 relativePos = lastPrimaryTarget.transform.position - transform.position;
Quaternion rotationtoTarget = Quaternion.LookRotation(relativePos, Vector3.up);
animator.SetIKRotationWeight(AvatarIKGoal.RightHand, finalLookWeight);
animator.SetIKRotation(AvatarIKGoal.RightHand, rotationtoTarget);
animator.SetIKPositionWeight(AvatarIKGoal.RightHand, finalLookWeight * 0.5f * closestLookWeight);
animator.SetIKPosition(AvatarIKGoal.RightHand, lastPrimaryTarget.transform.position);
}
if (finalLookWeight < lerpEndDistance)
{
transitionToNextTarget = false;
finalLookWeight = 0f;
lastPrimaryTarget = null;
}
}
// Show primary object found by the player
if (primaryTarget != null) text.text = "Item found: " + primaryTarget.description;
else text.text = "Item found: none";
// Show all objects found by the player
if (allDetectedItems.Count > 0)
{
string result = "";
foreach (InteractableItem item in allDetectedItems)
{
result += item.description + "\n";
}
textMultiLine.text = result;
}
else
{
textMultiLine.text = "No items found.";
}
}
}
private void ThrowObject()
{
if(target.interactableMode.)
objToThrow.transform.position = Vector3.MoveTowards(objToThrow.transform.position, target.transform.position, throwSpeed * Time.deltaTime);
}
private void Update()
{
if (throwObj == true)
{
ThrowObject();
}
}
}
在 ThrowObject 方法的底部,我希望它仅在对象枚举模式设置为 Action 时才抛出对象:
private void ThrowObject()
{
if(target.interactableMode.)
objToThrow.transform.position = Vector3.MoveTowards(objToThrow.transform.position, target.transform.position, throwSpeed * Time.deltaTime);
}
问题是我可以访问 IF 中的变量 target.interactableMode 但不能访问它自己的枚举。我想检查目标是否处于动作模式然后抛出对象。
解决方案
枚举在一个类中,所以要调用她,你必须先调用类,然后是枚举:YourClass.YourEnum
在您的情况下:InteractableItem.InteractableMode
并访问此枚举中的值:InteractableItem.InteractableMode.Action
推荐阅读
- python - 将重试循环嵌套到 try except 块 python API
- git - Git 显示两个 git-diff 的区别
- fortify - 批量强化 SSC Rest API GET 问题详细信息
- opencv - 单应矩阵计算
- installation - 终端:“找不到命令”
- java - 使用 BindingResult 进行 Spring MVC 集成测试
- sql-server - 如何在 ROW_NUMBER() TSQL 中使用过滤
- php - 执行 CURL 请求时出错:资源 ID #8
- javascript - ChartJS - 根据悬停数据显示百分比(AngularJS)
- vuejs2 - 如何让手动添加的 Vue 组件出现在 Vue 开发工具中?