You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
261 lines
8.1 KiB
261 lines
8.1 KiB
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using AX.Common; |
|
using System; |
|
using DevelopEngine; |
|
using UnityEngine.UI; |
|
//Author:YHD |
|
public class NodeSecondLevelControl : SingletonMono<NodeSecondLevelControl> |
|
{ |
|
#region Variables |
|
public ObjectPool<GameObject> nodePool; |
|
public Func<GameObject> creator; |
|
public Action<GameObject> clearer; |
|
private int nodeCount = 0; |
|
private GameObject nodePrefab; |
|
private Transform ContentParent; |
|
private List<TreeViewData> nodeDataList; |
|
public NodeItem_SL curNodeItem_SL; |
|
private string lastNodeName; |
|
private TagsControl TagsControl; |
|
private GameObject AddNodeBtn; |
|
private GameObject SaveNodeBtn; |
|
#endregion |
|
|
|
#region Mono Methods |
|
// Use this for initialization |
|
void Awake() |
|
{ |
|
nodePrefab = Resources.Load<GameObject>("UIPrefab/NodeItem_SL"); |
|
ContentParent = TransformHelper.FindChild(transform, "ToolGrid"); |
|
nodeDataList = new List<TreeViewData>(); |
|
nodeCount = ContentParent.childCount; |
|
TagsControl = TransformHelper.FindChild(transform, "TagsScrollView").GetComponent<TagsControl>(); |
|
creator = () => { var node = Instantiate(nodePrefab); node.SetActive(true); return node; }; |
|
clearer = (obj) => { obj.SetActive(false);obj.GetComponent<Toggle>().isOn = false; }; |
|
nodePool = new ObjectPool<GameObject>(8, creator, clearer); |
|
AddNodeBtn = TransformHelper.FindChild(transform, "AddNodeBtn").gameObject; |
|
SaveNodeBtn = TransformHelper.FindChild(transform, "SaveNodeBtn").gameObject; |
|
} |
|
void Start() |
|
{ |
|
|
|
ExamInfoHelpClass.NameListWorkPath = Application.dataPath + @"/ExtendFolder/xml/SaveCoursewares/345/" + "NodeNameList.xml"; |
|
GenerateNodeAccordingFirstLevelID(7); |
|
AddNodeBtn.GetComponent<Button>().onClick.AddListener(AddNodeBtnClick); |
|
SaveNodeBtn.GetComponent<Button>().onClick.AddListener(SaveNodeBtnClick); |
|
} |
|
|
|
|
|
// Update is called once per frame |
|
void Update() |
|
{ |
|
if (Input.GetKeyDown(KeyCode.F1)) |
|
{ |
|
GenerateNodeAccordingFirstLevelID(1); |
|
} |
|
else if (Input.GetKeyDown(KeyCode.F2)) |
|
{ |
|
GenerateNodeAccordingFirstLevelID(2); |
|
} |
|
else if (Input.GetKeyDown(KeyCode.F3)) |
|
{ |
|
GenerateNodeAccordingFirstLevelID(3); |
|
} |
|
else if (Input.GetKeyDown(KeyCode.F4)) |
|
{ |
|
GenerateNodeAccordingFirstLevelID(4); |
|
} |
|
else if (Input.GetKeyDown(KeyCode.F7)) |
|
{ |
|
GenerateNodeAccordingFirstLevelID(7); |
|
} |
|
} |
|
#endregion |
|
|
|
#region Others Methods |
|
|
|
private void AddNodeBtnClick() |
|
{ |
|
SetBtn(true); |
|
NodeSet.Instance.StrartRecord_new(); |
|
} |
|
private void SaveNodeBtnClick() |
|
{ |
|
SetBtn(false); |
|
NodeSet.Instance.StrartRecord_new(false); |
|
} |
|
|
|
void SetBtn(bool addFlag) |
|
{ |
|
AddNodeBtn.SetActive(!addFlag); |
|
SaveNodeBtn.SetActive(addFlag); |
|
} |
|
public void AddNode(string nodeName, int parentID, int id) |
|
{ |
|
TreeViewData data = new TreeViewData(); |
|
data.Name = nodeName; |
|
//data.ID = GetComponent<TreeViewItem__>().Controler.GetNewID(); |
|
data.ID = id; |
|
data.ParentID = parentID; |
|
data.Hierarchy = 2; |
|
|
|
var node = nodePool.Acquire(); |
|
node.transform.SetParent(ContentParent); |
|
node.GetComponent<NodeItem_SL>().SetData(data); |
|
if (curNodeItem_SL != null) |
|
{ |
|
node.transform.SetSiblingIndex(curNodeItem_SL.transform.GetSiblingIndex() + 1); |
|
NodeSet.Instance.InsertXml(curNodeItem_SL.GetData().ID.ToString(), data); |
|
} |
|
else |
|
{ |
|
NodeSet.Instance.ADDXml(nodeName, data); |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 设置当前选中的节点物体 |
|
/// </summary> |
|
/// <param name="node"></param> |
|
public void SelectCurNode(NodeItem_SL node, bool showTagsFlag = true) |
|
{ |
|
curNodeItem_SL = node; |
|
TagsControl.gameObject.SetActive(showTagsFlag); |
|
if (showTagsFlag) |
|
{ |
|
var curName = curNodeItem_SL.GetData().Name; |
|
TagsControl.GetComponent<RectTransform>().anchoredPosition = new Vector2( |
|
curNodeItem_SL.GetComponent<RectTransform>().anchoredPosition.x + |
|
ContentParent.GetComponent<RectTransform>().anchoredPosition.x, |
|
TagsControl.GetComponent<RectTransform>().anchoredPosition.y); |
|
if (curName != lastNodeName) |
|
{ |
|
TagsControl.GenerateTagAccordingNode(curName, curNodeItem_SL); |
|
} |
|
lastNodeName = curName; |
|
TempDataManager.Instance.CurNodeID = node.GetData().ID; |
|
} |
|
else |
|
{ |
|
HorizontalTreeSet.instance_.GetID(); |
|
} |
|
} |
|
/// <summary> |
|
/// 切换选中的NodtItem |
|
/// </summary> |
|
/// <param name="ID"></param> |
|
public void SetToggle(int ID) |
|
{ |
|
foreach (Transform child in ContentParent) |
|
{ |
|
if (child.GetComponent<NodeItem_SL>().GetData().ID == ID) |
|
{ |
|
child.GetComponent<NodeItem_SL>().SetToggle(); |
|
} |
|
} |
|
} |
|
/// <summary> |
|
/// 获取选中物体 |
|
/// </summary> |
|
/// <param name="GameObject"></param> |
|
public GameObject GetToggleGame() |
|
{ |
|
foreach (Transform child in ContentParent) |
|
{ |
|
if (child.GetComponent<Toggle>().isOn) |
|
{ |
|
return child.gameObject; |
|
} |
|
} |
|
return null; |
|
} |
|
/// <summary> |
|
/// 检测当前选中的前后有没有物体 |
|
/// </summary> |
|
/// <param name="Index"></param> |
|
/// <returns></returns> |
|
public GameObject CheckGetSiblingIndex(int Index) |
|
{ |
|
foreach (Transform child in ContentParent) |
|
{ |
|
if (child.GetSiblingIndex() + 1 == Index) |
|
{ |
|
return child.gameObject; |
|
} |
|
if (child.GetSiblingIndex() - 1 == Index) |
|
{ |
|
return child.gameObject; |
|
} |
|
} |
|
return null; |
|
} |
|
/// <summary> |
|
/// 获取父物体 |
|
/// </summary> |
|
/// <returns></returns> |
|
public GameObject GetFather() |
|
{ |
|
return ContentParent.gameObject; |
|
} |
|
|
|
/// <summary> |
|
/// 根据父层级ID刷新子节点 |
|
/// Tips:最好在Start中再调用,避免过早调用报null |
|
/// </summary> |
|
/// <param name="pid">父层级ID</param> |
|
public void GenerateNodeAccordingFirstLevelID(int pid) |
|
{ |
|
GetNodeDatas(pid); |
|
nodeCount = ContentParent.childCount; |
|
int index1 = 0, index2 = 0; |
|
var flag = nodeCount >= nodeDataList.Count;//现有node数量足够,无需新生成node,更新数据即可;否则需要新生成节点 |
|
index1 = flag ? nodeDataList.Count : nodeCount; |
|
index2 = !flag ? nodeDataList.Count : nodeCount; |
|
|
|
for (int i = 0; i < index1; i++) |
|
{ |
|
SetNode(i); |
|
} |
|
for (int i = index1; i < index2; i++) |
|
{ |
|
if (flag) |
|
{ |
|
nodePool.Release(ContentParent.GetChild(i).gameObject); |
|
} |
|
else |
|
{ |
|
var node = nodePool.Acquire(); |
|
node.transform.SetParent(ContentParent); |
|
node.GetComponent<NodeItem_SL>().SetData(nodeDataList[i]); |
|
node.transform.localScale = new Vector3(1, 1, 1); |
|
} |
|
} |
|
curNodeItem_SL = null; |
|
} |
|
/// <summary> |
|
/// 设置节点物体数据 |
|
/// </summary> |
|
/// <param name="i"></param> |
|
void SetNode(int i) |
|
{ |
|
var NodeItem_SL = ContentParent.GetChild(i).GetComponent<NodeItem_SL>(); |
|
NodeItem_SL.SetData(nodeDataList[i]); |
|
NodeItem_SL.gameObject.SetActive(true); |
|
} |
|
|
|
/// <summary> |
|
/// 根据父层级ID获取节点数据 |
|
/// </summary> |
|
/// <param name="id"></param> |
|
private void GetNodeDatas(int pid) |
|
{ |
|
List<TreeViewData> datas = new List<TreeViewData>(); |
|
NodeSet.Instance.GetAllDates(ref datas); |
|
nodeDataList.Clear(); |
|
datas.ForEach((d) => { if (d.ParentID == pid) { nodeDataList.Add(d); } }); |
|
} |
|
#endregion |
|
|
|
}
|
|
|