博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
红点类
阅读量:6675 次
发布时间:2019-06-25

本文共 9180 字,大约阅读时间需要 30 分钟。

using UnityEngine;using System.Collections;using System.Collections.Generic;using System.Text;using System.Text.RegularExpressions;using UnityEngine.Events;using UnityEngine.UI;namespace XGUI{
public class XHotSpot : MonoBehaviour {
public class HotSpotNode {
public string nodeName {
get; set; } public bool active {
get; set; } public int depth {
get; set; } public XHotSpot target {
get; set; } public List
targets {
get; set; } public HotSpotNode parent {
get; set; } public List
childs {
get; set; } public void SetActive(bool v) {
if (v == active) return; if (childs != null && childs.Count > 0) {
Debug.LogFormat("HotSpotNode::SetActive {0} 此节点是根据子节点状态改变不可直接设置!", nodeName); return; } SetActiveInternal(v); } private void SetActiveInternal(bool v) {
active = v; if (targets != null) {
foreach (var hs in targets) {
hs.m_HotActive = active; hs.OnHotChange(this); } } else if (target != null) {
target.m_HotActive = active; target.OnHotChange(this); } //通知父节点 if (parent != null) {
foreach (var node in parent.childs) {
if (node.active) {
parent.SetActiveInternal(true); return; } } parent.SetActiveInternal(false); } //设置子节点 不能设置需要依靠子节点信息 //if (childs != null && childs.Count > 0) //{
// foreach (var childNode in childs) // childNode.SetActive(active); //} } //子节点的激活数量 public int GetActiveChildCount() {
int count = 0; if (childs != null && childs.Count > 0) {
foreach (var childNode in childs) {
if (childNode.active) ++count; } } return count; } //子节点的激活nodeName public List
GetActiveChildNodeName() {
List
list = new List
(); if (childs != null && childs.Count > 0) {
foreach (var childNode in childs) {
if (childNode.active) list.Add(childNode.nodeName); } } return list; } public override string ToString() {
StringBuilder sb = new StringBuilder(); sb.AppendFormat("hotName[{0}] target:[{1}] child:[{2}] {3}\n", nodeName, targets != null ? targets.Count : (target != null ? 1 : 0), childs != null ? childs.Count : 0, active ? "√" : ""); if (childs != null) {
foreach (var child in childs) {
for (int i = 0; i < depth; i++) {
sb.Append(" "); } sb.AppendFormat("|_ {0}", child.ToString()); } } return sb.ToString(); } } //--------------------------------------------------------- static start ------------------------------------------------------------------------------ public static UnityAction s_OnChange; public static Dictionary
s_RootNodes = new Dictionary
(); private static Dictionary
m_HotSpotMap = new Dictionary
(); static Regex s_dotRegex = new Regex("\\/"); static List
s_temp = new List
(); static List
GetNodePaths(string hotName) { if (s_temp.Count > 0) s_temp.Clear(); MatchCollection mc = s_dotRegex.Matches(hotName); for (int i = 0; i < mc.Count; i++) s_temp.Add(hotName.Substring(0, mc[i].Index)); s_temp.Add(hotName); return s_temp; } private static HotSpotNode BuildNode(string hotSoptPath) { List
paths = GetNodePaths(hotSoptPath); HotSpotNode parent = null; HotSpotNode curNode = null; int depth = 0; foreach (var nodeName in paths) { depth++; if (!m_HotSpotMap.TryGetValue(nodeName, out curNode)) { curNode = new HotSpotNode { nodeName = nodeName, depth = depth }; m_HotSpotMap.Add(nodeName, curNode); if (parent != null) { if (parent.childs == null) parent.childs = new List
(); parent.childs.Add(curNode); curNode.parent = parent; } } if (nodeName == paths[0] && !s_RootNodes.ContainsKey(nodeName)) s_RootNodes.Add(nodeName, curNode); parent = curNode; } return curNode; } private static void AddHotSpot(XHotSpot hotSpot) { if (string.IsNullOrEmpty(hotSpot.hotName)) { XLogger.WARNING(string.Format("XHotSpot::AddHotSpot hotName is null {0}", hotSpot)); return; } HotSpotNode node = null; if (!m_HotSpotMap.TryGetValue(hotSpot.hotName, out node)) { node = BuildNode(hotSpot.hotName); CallChangeEvent(); } if (node.target == hotSpot || (node.targets != null && node.targets.Contains(hotSpot))) return; if (node.targets != null) { //多个对象同属一个热点 node.targets.Add(hotSpot); } else if (node.target != null) { //多个对象同属一个热点 node.targets = new List
(); node.targets.Add(node.target); node.targets.Add(hotSpot); node.target = null; } else { node.target = hotSpot; } if (hotSpot.m_HotActive != node.active) { hotSpot.m_HotActive = node.active; hotSpot.OnHotChange(node); } } private static void RemoveHotSpot(XHotSpot hotSpot) { if (string.IsNullOrEmpty(hotSpot.hotName)) { XLogger.WARNING(string.Format("XHotSpot::RemoveHotSpot hotName is null {0}", hotSpot)); return; } HotSpotNode node = null; if (m_HotSpotMap.TryGetValue(hotSpot.hotName, out node)) { if (node.targets != null && node.targets.Contains(hotSpot)) { node.targets.Remove(hotSpot); } node.target = null; } } public static string ToStringInfo() { StringBuilder sb = new StringBuilder(); foreach (var node in s_RootNodes) { sb.Append(node.Value.ToString()); } return sb.ToString(); } public static void SetHotSpotActive(string hotName, bool v) { if (string.IsNullOrEmpty(hotName)) { XLogger.WARNING(string.Format("XHotSpot::SetHotSpotActive hotName is null {0}", v)); return; } HotSpotNode node = null; if (!m_HotSpotMap.TryGetValue(hotName, out node)) { node = BuildNode(hotName); CallChangeEvent(); } if (node.active == v) { return; } node.SetActive(v); } public static HotSpotNode GetNodeByHotName(string hotName) { HotSpotNode curNode = null; s_RootNodes.TryGetValue(hotName, out curNode); return curNode; } public static bool IsShowing(string hotName) { if (hotName == null) { return false; } HotSpotNode curNode = null; m_HotSpotMap.TryGetValue(hotName, out curNode); if (curNode != null) return curNode.active; else return false; } public static void DefaultHotSpotMap() { foreach (KeyValuePair
kv in m_HotSpotMap) { if (kv.Value.childs != null && kv.Value.childs.Count > 0) { continue; }else { if (kv.Value.active) { kv.Value.SetActive(false); } } } } private static void CallChangeEvent() { if (s_OnChange != null) s_OnChange.Invoke(); } //--------------------------------------------------------- static end ------------------------------------------------------------------------------ [SerializeField] private string m_HotName; //显示图片 [SerializeField] private XImage m_TargetImage; //需要显示的数字 [SerializeField] private Text m_TargetText; //自身状态更新 public UnityAction
OnStateChange; private bool m_HotActive; public bool hotActive { get { return m_HotActive; } } public XImage targetImage { get { return m_TargetImage; } } public Text targetText { get { return m_TargetText; } } public string hotName { get { return m_HotName; } set { if (m_HotName == value) return; if (!string.IsNullOrEmpty(m_HotName)) { RemoveHotSpot(this); } m_HotName = value; if (!string.IsNullOrEmpty(m_HotName)) { AddHotSpot(this); } else { if (m_TargetImage) m_TargetImage.visible = false; m_HotActive = false; } } } private void Awake() { if (m_TargetImage) m_TargetImage.visible = false; if (m_TargetText) m_TargetText.enabled = false; if (!string.IsNullOrEmpty(m_HotName)) AddHotSpot(this); } private void OnDisable() { hotName = string.Empty; } private void OnDestroy() { OnStateChange = null; } public void SetHotActive(bool v) { SetHotSpotActive(m_HotName, v); } //状态发生改变 public virtual void OnHotChange(HotSpotNode node) { if (m_TargetImage) { m_TargetImage.visible = node.active; } if (m_TargetText) { m_TargetText.enabled = node.active; m_TargetText.text = node.GetActiveChildCount().ToString(); } if (OnStateChange != null) OnStateChange.Invoke(node.active); } }}

转载地址:http://gfrxo.baihongyu.com/

你可能感兴趣的文章
分享一个快速开发jQuery插件工具:jqueryboilerplate(转)
查看>>
Training的第二十天
查看>>
mysql设置主键自动增长
查看>>
linux系统的启动过程
查看>>
MySQL性能分析
查看>>
IIS错误日志 事件ID: 1093
查看>>
解决Unable to resolve target 'android-7'报错
查看>>
Connections could not be acquired from the unde...
查看>>
UIAlertView 总结
查看>>
邮件服务器:SMTP协议原始命令码和工作原理
查看>>
在Sublime Text中配置 JavaScript 控制台(JavaScript Console in Sublime Text)
查看>>
python使用os模块获取当前目录
查看>>
DNS服务(一)——DNS原理及其解析过程详解
查看>>
卸载linux软件总结
查看>>
redhat 6.5 安装和配置zabbix客户端
查看>>
硬链接和软链接(2)
查看>>
几种REST服务JAVA客户端类库
查看>>
什么是Hijax?Hijax的原理及优缺点介绍
查看>>
【2016-03-17】移动互联网时代,看好你的隐私
查看>>
linux命令:编译安装postfix邮件服务
查看>>