private IEnumerator CreateItem()
    {
        if (data == null)
        {
            yield break;
        }
        ChapterId          = data.GetData <int>(ConstDefine.ChapterId);
        m_ChapterName.text = data.GetData <string>(ConstDefine.ChapterName);
        string bgName = data.GetData <string>(ConstDefine.ChapterBG);

        m_ChapterBG.texture = GameUtil.LoadGameLevelMapBG(bgName);
        List <DataTransfer> dataArr = data.GetData <List <DataTransfer> >(ConstDefine.GameLevelArr);

        mapItems.Clear();
        for (int i = 0; i < dataArr.Count; i++)
        {
            GameObject go  = ResourcesMrg.Instance.Load(ResourcesMrg.ResourceType.UIWindowChild, "GameLevelItem");
            Vector2    pos = dataArr[i].GetData <Vector2>(ConstDefine.GameLevelPosInMap);
            go.transform.SetParent(m_ChapterBG.transform);
            go.transform.localPosition    = pos;
            go.transform.localEulerAngles = Vector3.zero;
            go.transform.localScale       = Vector3.one;
            UIGameLevelItemView itemView = go.GetComponent <UIGameLevelItemView>();
            itemView.OnClickGameLevelItem = ClickGameLevelItem;
            itemView.SetUI(dataArr[i]);
            mapItems.Add(go.transform);
            yield return(null);
        }

        for (int i = 0; i < mapItems.Count; i++)
        {
            if (i == mapItems.Count - 1)
            {
                break;
            }
            Vector3 beginPos = mapItems[i].transform.localPosition;
            Vector3 endPos   = mapItems[i + 1].transform.localPosition;
            float   distance = Vector3.Distance(beginPos, endPos);
            int     count    = (int)(distance / 20);
            float   xStep    = (endPos.x - beginPos.x) / count;
            float   yStep    = (endPos.y - beginPos.y) / count;
            for (int j = 0; j < count; j++)
            {
                if (j == count - 1)
                {
                    break;
                }
                GameObject go = ResourcesMrg.Instance.Load(ResourcesMrg.ResourceType.UIWindowChild, "GameLevelPointItem");
                go.transform.SetParent(m_PointContainer);
                go.transform.localPosition = new Vector3(beginPos.x + xStep * j, beginPos.y + yStep * j, 0);
                go.transform.localScale    = Vector3.one;
                go.transform.rotation      = Quaternion.identity;
                UIGameLevelMapPointView pointView = go.GetComponent <UIGameLevelMapPointView>();
                pointView.SetUI(false);
                yield return(null);
            }
        }
    }
Ejemplo n.º 2
0
    private IEnumerator LoadPoint(GameObject obj)
    {
        for (int i = 0; i < m_GameLevelItems.Count; i++)
        {
            if (i == m_GameLevelItems.Count - 1)
            {
                break;
            }

            //起始点
            Transform transBegin = m_GameLevelItems[i];

            //结束点
            Transform transEnd = m_GameLevelItems[i + 1];

            //计算两点的距离
            float distance = Vector2.Distance(transBegin.localPosition, transEnd.localPosition);

            //计算生成多少个连线点
            int createCount = Mathf.FloorToInt(distance / 20f);

            float xLen = transEnd.localPosition.x - transBegin.localPosition.x;
            float yLen = transEnd.localPosition.y - transBegin.localPosition.y;

            //xy的递增
            float stepX = xLen / createCount;
            float stepY = yLen / createCount;

            //创建点
            for (int j = 0; j < createCount; j++)
            {
                if (j < 1 || j > createCount - 1)
                {
                    continue;
                }

                //克隆点
                GameObject objPoint = Instantiate(obj);

                objPoint.SetParent(pointContainer);
                objPoint.transform.localPosition = new Vector3(transBegin.transform.localPosition.x + (stepX * j), transBegin.transform.localPosition.y + (stepY * j), 0f);

                UIGameLevelMapPointView view = objPoint.GetComponent <UIGameLevelMapPointView>();
                if (view != null)
                {
                    //todu:和服务器交互
                    view.SetUI(false);
                }

                yield return(null);
            }
        }
    }