Ejemplo n.º 1
0
    private void StopForceAll()
    {
        for (int i = gameObject.transform.childCount - 1; i >= 0; i--)
        {
            GameObject go = gameObject.transform.GetChild(i).gameObject;

            Adorning ba = go.GetComponent <Adorning>();
            if (ba != null)
            {
                ba.StopForce();
            }
        }
    }
Ejemplo n.º 2
0
    private void ApplyForceAll(Vector3 center)
    {
        for (int i = gameObject.transform.childCount - 1; i >= 0; i--)
        {
            GameObject go = gameObject.transform.GetChild(i).gameObject;

            Adorning ba = go.GetComponent <Adorning>();
            if (ba != null)
            {
                Vector3 dir = go.transform.position - center;
                float   len = dir.magnitude;
                if (len < DistanceThre)
                {
                    float alpha = 1.0f - Mathf.Clamp01(len / DistanceThre);
                    dir.Normalize();
                    dir *= (alpha * forcefactor);
                    ba.ApplyForce(center, dir, sizefactor);
                }
            }
        }
    }
Ejemplo n.º 3
0
    public void Build()
    {
        //RenderSettings.fog = false;
        //RenderSettings.fogColor = new Color(112.0f / 255.0f, 127.0f / 255.0f, 131.0f / 255.0f);
        //RenderSettings.fogStartDistance = 25.0f;
        //RenderSettings.fogEndDistance = 125.0f;

        //这里要先加到一个临时列表,因为新生成的groupobj也是当前gameobject.transform的子对象
        List <Transform> objs = new List <Transform>();
        //Renderer[] mrs = transform.GetComponentsInChildren<Renderer>();
        //if (mrs == null)
        //{
        //    return;
        //}
        //int n = mrs.Length;
        //for (int i = 0; i < n; i++)
        //{
        //    Transform tchild = mrs[i].transform;
        //    objs.Add(tchild);
        //}

        int n = transform.childCount;

        for (int i = 0; i < n; i++)
        {
            Transform tchild = transform.GetChild(i);
            if (tchild.lossyScale.x > 0.1f && tchild.lossyScale.z > 0.1f)//对于缩放系数为负的物件只能整体处理,unity对于缩放为负处理可能有bug
            {
                Renderer[] mrs = tchild.GetComponentsInChildren <Renderer>();
                foreach (Renderer mr in mrs)
                {
                    objs.Add(mr.transform);
                }
            }
            else
            {
                objs.Add(tchild);
            }
        }

        //对临时列表中的对象进行分组
        Hashtable groupobjs = new Hashtable();

        for (int i = 0; i < objs.Count; i++)
        {
            Transform    tchild = objs[i];
            Vector3      pos    = tchild.position;
            MeshRenderer mr     = tchild.gameObject.GetComponent <MeshRenderer>();
            if (mr != null && mr.bounds != null)
            {
                pos = mr.bounds.center;
            }
            Vector3 vrelate = pos - vmin;
            int     x       = (int)(vrelate.x / vdistance.x);
            int     y       = (int)(vrelate.z / vdistance.z);

            x -= groupsize;
            y -= groupsize;

            if (x < -groupsize)
            {
                x = -groupsize;
            }
            if (y < -groupsize)
            {
                y = -groupsize;
            }
            if (x > groupsize)
            {
                x = groupsize;
            }
            if (y > groupsize)
            {
                y = groupsize;
            }

            GameObject gogroup = null;
            int        key     = GetKeyByGroup(x, y);
            if (!groupobjs.ContainsKey(key))
            {
                gogroup        = CreateGroup(x, y);
                groupobjs[key] = gogroup;
            }
            else
            {
                gogroup = (GameObject)groupobjs[key];
            }

            tchild.parent = gogroup.transform;
        }

        // 对分组对象构建AdorningGroup脚本
        foreach (DictionaryEntry keypair in groupobjs)
        {
            int        key     = (int)keypair.Key;
            GameObject gogroup = (GameObject)keypair.Value;

            int stride = groupsize * 2 + 1;
            int x      = key % stride;
            int y      = key / stride;

            AdorningGroup ag = gogroup.AddComponent <AdorningGroup>();
            ag.x        = x - groupsize;
            ag.y        = y - groupsize;
            ag.pos      = gogroup.transform.position;
            groups[key] = ag;
        }

        // 合并每个分组中的网格,减少批次
        foreach (DictionaryEntry keypair in groups)
        {
            int           key = (int)keypair.Key;
            AdorningGroup ag  = (AdorningGroup)keypair.Value;
            //ag.Combine();
        }

        // 加到列表
        foreach (DictionaryEntry keypair in groups)
        {
            int           key     = (int)keypair.Key;
            AdorningGroup ag      = (AdorningGroup)keypair.Value;
            GameObject    gogroup = ag.gameObject;

            for (int i = 0; i < gogroup.transform.childCount; i++)
            {
                Transform tchild   = gogroup.transform.GetChild(i);
                Adorning  adorning = tchild.gameObject.AddComponent <Adorning>();
                adorning.alpha = 2.0f;
                ag.objs.Add(adorning);
            }
        }

        //最后处理没分组的
        List <Transform> ungrouplist = new List <Transform>();

        for (int i = 0; i < transform.childCount; i++)
        {
            Transform tchild = transform.GetChild(i);
            if (tchild.gameObject.GetComponent <AdorningGroup>() == null)
            {
                ungrouplist.Add(tchild);
            }
        }
        GameObject ungroupparent = new GameObject("ungroup");

        ungroupparent.transform.parent = transform;
        foreach (Transform tchild in ungrouplist)
        {
            tchild.parent = ungroupparent.transform;
        }
    }