如何使用滑块从其生成器对象中的生成的对象更改属性 [英] How to change a property from a spawned object in its spawner object using a slider
本文介绍了如何使用滑块从其生成器对象中的生成的对象更改属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个附加了脚本(Defender
)的对象.该脚本具有defRadius
属性.另外,我使用以下代码在一个圆圈中生成对象:
I have an object with a script (Defender
) attached to it. This script has a defRadius
property. Also I am spawning my object in a circle using the following code:
CircleSpawn
public class CircleSpawn : MonoBehaviour
{
public float radius, radiusLast, spin, spinLast;
public int numOfItems;
public GameObject clonedObject;
public List<GameObject> spawnedObjects;
}
CircleSpawnEditor
[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;
}
}
}
}
如何生成用defRadius
(将Defender
脚本的属性附加到要克隆的对象上)替换CircleSpawn
中的radius
的对象?
How can I spawn the objects replacing radius
in CircleSpawn
with the defRadius
(property of the Defender
script attached to the object to be cloned)?
推荐答案
要从原始预制件读取和写入值,我们首先保存对新脚本Defender
的引用,并在原始代码中替换tar.radius
:
To read and write values from the original prefab, we first save a reference to the new script Defender
and replace tar.radius
in the original code:
public class Defender : MonoBehaviour
{
public float Radius;
}
[CustomEditor(typeof(CircleSpawn))]
public class CircleSpawnEditor : Editor
{
private Defender _defender;
public override void OnInspectorGUI ()
{
var tar = (CircleSpawn)target;
tar.clonedObject = (GameObject)EditorGUILayout.ObjectField(tar.clonedObject,
typeof(GameObject), true);
if (!tar.clonedObject) return;
if (!_defender)
{
_defender = tar.clonedObject.GetComponent<Defender>();
if (!_defender) return;
}
EditorGUILayout.LabelField("Radius"); // Set as required
_defender.Radius = EditorGUILayout.Slider(_defender.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");
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 * _defender.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(_defender.Radius, tar.radiusLast))
{
tar.spinLast = tar.spin;
tar.radiusLast = _defender.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 * _defender.Radius;
tar.spawnedObjects[i].transform.position =
tar.transform.position + localPos;
tar.spawnedObjects[i].transform.rotation = rot;
angle += angleBetween;
}
}
}
}
这篇关于如何使用滑块从其生成器对象中的生成的对象更改属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文