93 lines
2.6 KiB
C#
93 lines
2.6 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
//============================================================
|
||
//支持中文,文件使用UTF-8编码
|
||
//@author #AUTHOR#
|
||
//@create #CREATEDATE#
|
||
//@company #COMPANY#
|
||
//
|
||
//@description:
|
||
//============================================================
|
||
|
||
public class UPosManger : MonoBehaviour
|
||
{
|
||
public UPosItem[] uPosItems;
|
||
public List<UPosItem> currentUPosIsOccupied = new List<UPosItem>();
|
||
// Use this for initialization
|
||
private void Start()
|
||
{
|
||
uPosItems = GetComponentsInChildren<UPosItem>();
|
||
SetCurrentUPosIsOccupied(0, uPosItems.Length - 1, false);
|
||
}
|
||
|
||
[ContextMenu("SetUPosItem")]
|
||
public void SetUPosItem()
|
||
{
|
||
for (int i = 0; i < transform.childCount; i++)
|
||
{
|
||
//DestroyImmediate(transform.GetChild(i).gameObject.GetComponent<UPosItem>());
|
||
transform.GetChild(i).gameObject.GetComponent<UPosItem>().SetValue();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 监视所有U位
|
||
/// </summary>
|
||
public void SwitchUItem()
|
||
{
|
||
for (int i = 0; i < uPosItems.Length; i++)
|
||
{
|
||
uPosItems[i].isOccupied = false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 计算是否符合放置条件
|
||
/// </summary>
|
||
public int CountUPos(int count, int currentUPosIndex)
|
||
{
|
||
int startIndex = currentUPosIndex - count;
|
||
if (startIndex < 0) startIndex = 0;
|
||
for (int i = currentUPosIndex - 1; i >= startIndex; i--)
|
||
{
|
||
if (uPosItems[i].isOccupied)
|
||
{
|
||
startIndex = i + 1;
|
||
break;
|
||
//currentUPosIsOccupied.Add(uPosItems[i]);
|
||
//uPosItems[i].isOccupied = true;
|
||
}
|
||
}
|
||
|
||
if (startIndex + count > uPosItems.Length) return -1;
|
||
for (int i = startIndex; i < startIndex + count; i++)
|
||
{
|
||
if (uPosItems[i].isOccupied)
|
||
return -1;
|
||
}
|
||
|
||
return startIndex;
|
||
//return startIndex + count - 1;
|
||
}
|
||
|
||
|
||
public void SetCurrentUPosIsOccupied(int startIndex, int count, bool isEnabel)
|
||
{
|
||
//Debug.Log("startIndex-------------" + startIndex);
|
||
//Debug.Log("count-------------" + count);
|
||
for (int i = startIndex; i < startIndex + count; i++)
|
||
{
|
||
uPosItems[i].isOccupied = isEnabel;
|
||
if (isEnabel)
|
||
{
|
||
uPosItems[i].SetInstructColor(Color.red);
|
||
}
|
||
else
|
||
{
|
||
uPosItems[i].SetInstructColor(Color.green);
|
||
}
|
||
}
|
||
}
|
||
}
|