C# 如何使用滑块从其衍生对象中的衍生对象更改属性
我有一个附加了脚本(C# 如何使用滑块从其衍生对象中的衍生对象更改属性,c#,unity3d,C#,Unity3d,我有一个附加了脚本(Defender)的对象。此脚本具有defRadius属性。此外,我正在使用以下代码生成圆形中的对象: CircleSpawn public class CircleSpawn : MonoBehaviour { public float radius, radiusLast, spin, spinLast; public int numOfItems; public GameObject clonedObject; public List&l
Defender
)的对象。此脚本具有defRadius
属性。此外,我正在使用以下代码生成圆形中的对象:
CircleSpawn
public class CircleSpawn : MonoBehaviour
{
public float radius, radiusLast, spin, spinLast;
public int numOfItems;
public GameObject clonedObject;
public List<GameObject> spawnedObjects;
}
如何生成将
CircleSpawn
中的radius
替换为defRadius
(附加到要克隆对象的Defender
脚本的属性)的对象?从原始预置读取和写入值,我们首先保存对新脚本Defender
的引用,并在原始代码中替换tar.radius
:
public class Defender:monobhavior
{
公众浮标半径;
}
[CustomEditor(typeof(CircleSpawn))]
公共类CirclePawneditor:编辑
{
私人辩护律师;
InspectorGUI()上的公共覆盖无效
{
var tar=(CircleSpawn)目标;
tar.clonedObject=(游戏对象)EditorGUILayout.ObjectField(tar.clonedObject,
typeof(游戏对象),true);
如果(!tar.clonedObject)返回;
如果(!\u后卫)
{
_defender=tar.clonedObject.GetComponent();
如果(!\u defender)返回;
}
EditorGUILayout.LabelField(“半径”);//根据需要设置
_defender.Radius=EditorGUILayout.Slider(_defender.Radius,0f,100f);
EditorGUILayout.LabelField(“角度”);//根据需要设置
tar.spin=EditorGUILayout.Slider(tar.spin,0f,360f);
EditorGUILayout.LabelField(“项目数”);//根据需要设置
tar.numOfItems=EditorGUILayout.IntSlider(tar.numOfItems,0,36);
EditorGUILayout.LabelField(“对象”);
浮动角度,角度介于=360.0f/tar.numOfItems之间;
if(tar.spawnedObject==null)
tar.spawnedObjects=新列表();
//解决方案#1
if(tar.spawnedObjects.Count!=tar.numOfItems)
{
foreach(tar.spawnedObject中的变量ob)
立即死亡(ob);
tar.spawnedObject.Clear();
角度=0f;
对于(int i=0;i
嘿,你只想更改原始预置的值,对吗?@Lece是的。通常在CircleSpawn
中更改半径的值,这能回答您的问题吗?@Lece precisselyexcellent!如果您希望将其他变量(spin/numItems)保存到原始预置,只需复制我对radius所做的操作:)
[CustomEditor(typeof(CircleSpawn))]
public class CircleSpawnEditor : Editor
{
public override void OnInspectorGUI ()
{
var tar = (CircleSpawn)target;
EditorGUILayout.LabelField("Radius"); // Set as required
tar.radius = EditorGUILayout.Slider(tar.radius, 0f, 100f);
EditorGUILayout.LabelField("Angle"); // Set as required
tar.spin = EditorGUILayout.Slider(tar.spin, 0f, 360f);
EditorGUILayout.LabelField("Number of Items"); // Set as required
tar.numOfItems = EditorGUILayout.IntSlider(tar.numOfItems, 0, 36);
EditorGUILayout.LabelField("Object");
tar.clonedObject = (GameObject)EditorGUILayout.ObjectField(tar.clonedObject,
typeof(GameObject), true);
float angle, angleBetween = 360.0f / tar.numOfItems;
if (tar.spawnedObjects == null)
tar.spawnedObjects = new List<GameObject>();
// Solution #1
if (tar.spawnedObjects.Count != tar.numOfItems)
{
foreach (var ob in tar.spawnedObjects)
DestroyImmediate(ob);
tar.spawnedObjects.Clear();
angle = 0f;
for (int i = 0; i < tar.numOfItems; i++)
{
var rot = Quaternion.Euler(0f, 0f, tar.spin + angle);
var localPos = rot * Vector3.right * tar.radius;
tar.spawnedObjects.Add(Instantiate(tar.clonedObject,
tar.transform.position + localPos, rot));
angle += angleBetween;
}
}
// Solutions #2 & 3
if (!Mathf.Approximately(tar.spin, tar.spinLast) ||
!Mathf.Approximately(tar.radius, tar.radiusLast))
{
tar.spinLast = tar.spin;
tar.radiusLast = tar.radius;
angle = 0f;
for (int i = 0; i < tar.numOfItems; i++)
{
var rot = Quaternion.Euler(0f, 0f, tar.spin + angle);
var localPos = rot * Vector3.right * tar.radius;
tar.spawnedObjects[i].transform.position =
tar.transform.position + localPos;
tar.spawnedObjects[i].transform.rotation = rot;
angle += angleBetween;
}
}
}
}