代码之家  ›  专栏  ›  技术社区  ›  funseiki

统一-更新禁用游戏对象的顶点颜色

  •  0
  • funseiki  · 技术社区  · 4 年前

    this video )这会改变玩家变换附近平面上网格的某一组顶点的alpha值。

    void FixedUpdate()
    { 
        for(int i = 0; i < vertices.Length; i++)
        { 
            Vector3 v = fogPlane.transform.TransformPoint(vertices[i]);
            Vector3 playerOnMap = new Vector3(player.position.x, v.y, player.position.z);
            float dist = Vector3.SqrMagnitude(v - playerOnMap);
            // Set any vertices near the player to invisible
            if(dist < radiusSquared)
            {
                float alpha = Mathf.Min(colors[i].a, dist / radiusSquared);
                colors[i].a = alpha;
            }
        }
        mesh.colors = colors;
    }
    

    这看起来和 MeshFilter mesh 在启用之前未正确设置。将其设置为OnEnable适用于所有后续的玩家移动,但我也需要在捕获之前捕获移动。

    一个选项是在加载场景时启用然后禁用组件,但如果可能的话,我希望避免这种情况。不管这个平面是如何产生的,有没有一个好的方法来处理这个普遍的问题?

        void OnFogEnable()
        {
            // This will show false the first time it is Enabled
            Debug.Log("OnFogEnabled. Mesh match? " + (mesh == fogPlane.GetComponent<MeshFilter>().mesh));
            Debug.Log("OnFogEnabled. Vertices match? " + (vertices == mesh.vertices));
           
            mesh = fogPlane.GetComponent<MeshFilter>().mesh;
            vertices = mesh.vertices;
        }
    

    Fog行为的其余初始化代码如下所示:

        public GameObject fogPlane;
        public Transform player;
        public float radius = 5;
    
        private float radiusSquared { get { return radius * radius; } }
        private Mesh mesh;
        private Vector3[] vertices;
        private Color[] colors
    
        void Start()
        {
            mesh = fogPlane.GetComponent<MeshFilter>().mesh;
            vertices = mesh.vertices;
            colors = new Color[vertices.Length];
            for(int i = 0; i < colors.Length; i++)
                colors[i] = Color.black;
            mesh.colors = colors;
        }
    
    0 回复  |  直到 4 年前