Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何创建管理器脚本以同时控制所有绘制的圆?_C#_Unity3d - Fatal编程技术网

C# 如何创建管理器脚本以同时控制所有绘制的圆?

C# 如何创建管理器脚本以同时控制所有绘制的圆?,c#,unity3d,C#,Unity3d,第一个脚本用于绘制圆: 当我把这个脚本附加到一个游戏对象上时,它会在对象周围画一个圆圈 using UnityEngine; using System.Collections; using System.Collections.Generic; [RequireComponent(typeof(LineRenderer))] public class DrawCircle : MonoBehaviour { [Range(0, 50)] public int segments

第一个脚本用于绘制圆: 当我把这个脚本附加到一个游戏对象上时,它会在对象周围画一个圆圈

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[RequireComponent(typeof(LineRenderer))]
public class DrawCircle : MonoBehaviour
{
    [Range(0, 50)]
    public int segments = 50;
    [Range(1, 50)]
    public float xradius = 5;
    [Range(1, 50)]
    public float yradius = 5;
    [Range(-10, 10)]
    public float height = 0;
    public bool changeBothRadius = false;
    [Range(0.1f, 2)]
    public float lineThickness = 0.1f;
    public bool minimumRadius = false;

    private LineRenderer line;

    void Start()
    {
        line = gameObject.GetComponent<LineRenderer>();
        line.positionCount = segments + 1;
        line.useWorldSpace = false;
    }

    void Update()
    {
        line.startWidth = lineThickness;
        line.endWidth = lineThickness;

        CreatePoints();
    }

    void CreatePoints()
    {
        float x;
        float z;

        float angle = 20;

        for (int i = 0; i < (segments + 1); i++)
        {
            x = Mathf.Sin(Mathf.Deg2Rad * angle) * xradius;
            z = Mathf.Cos(Mathf.Deg2Rad * angle) * yradius;

            line.SetPosition(i, new Vector3(x, height, z));

            angle += (360f / segments + 1);
        }
    }
}
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
[RequiredComponent(typeof(LineRenderer))]
公共阶层拉丝圈:单一行为
{
[范围(0,50)]
公共整数段=50;
[射程(1,50)]
公共浮点数xradius=5;
[射程(1,50)]
公共浮动yradius=5;
[范围(-10,10)]
公共浮子高度=0;
公共bool changeBothRadius=false;
[范围(0.1f,2)]
公共浮子线宽=0.1f;
公共布尔最小半径=假;
专线;
void Start()
{
line=gameObject.GetComponent();
line.positionCount=段数+1;
line.useWorldSpace=false;
}
无效更新()
{
line.startWidth=线宽;
line.endWidth=线宽;
CreatePoints();
}
void CreatePoints()
{
浮动x;
浮动z;
浮动角度=20;
对于(int i=0;i<(段+1);i++)
{
x=最大Sin(最大Deg2Rad*角度)*x半径;
z=数学余弦(数学Deg2Rad*角)*矢面;
行。设置位置(i,新矢量3(x,高度,z));
角度+=(360f/段+1);
}
}
}
现在,我创建了一个管理器脚本,可以同时控制所有圆圈:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CirclesManager : MonoBehaviour
{
    public GameObject[] objectsToAddCircles;
    [Range(0, 50)]
    public int segments = 50;
    [Range(1, 50)]
    public float xradius = 5;
    [Range(1, 50)]
    public float yradius = 5;
    [Range(-10, 10)]
    public float height = 0;
    public bool changeBothRadius = false;
    [Range(0.1f, 2)]
    public float lineThickness = 0.1f;
    public bool minimumRadius = false;

    void Start()
    {

        for (int i = 0; i < objectsToAddCircles.Length; i++)
        {
            objectsToAddCircles[i].AddComponent<DrawCircle>();
            objectsToAddCircles[i].AddComponent<LineRenderer>();
        }
    }

    void Update()
    {
        for (int i = 0; i < objectsToAddCircles.Length; i++)
        {
            var lr = objectsToAddCircles[i].GetComponent<LineRenderer>();
            lr.startWidth = lineThickness;
            lr.endWidth = lineThickness;

            var dc = objectsToAddCircles[i].GetComponent<DrawCircle>();
            dc.segments = segments;
            dc.xradius = xradius;
            dc.yradius = yradius;
            dc.height = height;
            dc.changeBothRadius = changeBothRadius;
            dc.minimumRadius = minimumRadius;
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类CircleManager:MonoBehavior
{
公共游戏对象[]对象添加圆圈;
[范围(0,50)]
公共整数段=50;
[射程(1,50)]
公共浮点数xradius=5;
[射程(1,50)]
公共浮动yradius=5;
[范围(-10,10)]
公共浮子高度=0;
公共bool changeBothRadius=false;
[范围(0.1f,2)]
公共浮子线宽=0.1f;
公共布尔最小半径=假;
void Start()
{
for(int i=0;i
因此,现在每个对象都有LineRenderer组件和DrawCircle脚本,现在CircleManager脚本可以很好地处理所有对象,但如果我尝试更改单个对象设置,它将不会更改。例如,我可以在manager脚本中更改xrdaius或yradius滑块,但如果我尝试在特定对象中更改它们,则滑块不会移动也不会更改


无法弄清楚为什么管理器脚本可以工作,但不能使用脚本和LineRenderer处理每个对象。

在更新中的CircleManager类中,您有以下行:

dc.xradius = xradius;
dc.yradius = yradius;
无论您在何处以及如何更改单个
DrawCircle
实例半径,这些行始终会覆盖这些值

我不知道你想归档哪种行为,但你可以创建一个bool数组,这样你就可以手动设置哪些圈将由CircleManager驱动,哪些圈将使用自己的值:

// you can change it in the inspector which is handy
// if i'th value of this array is false
// then i'th CircleDrawer GameObject in objectsToAddCircles array
// won't be affected by this manager
public bool changableCircle[];

void Start() {
    // your code
    changableCircle = new bool[objectsToAddCircles.Length];
}

void Update() {
    for(...) {
        // values which are always overwritten by manager
        if(changableCircle[i]) {
            // values which you don't want to be changed by this manager
        }
    }
}

这是有效的。我想要的行为我认为这是合乎逻辑的。既可以同时控制所有的圆圈,也可以只控制一些和一些个人。能控制所有的对象,或者只控制一些对象,这难道不符合逻辑吗?