Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

c# - Is there an alternative way to Collider2D.CreateMesh in Unity 2019?

I am trying to make a game like Color Hole. I don't use blender to create hole. I am trying to use meshes to create hole on a ground. However Collider2D.CreateMesh method is not working because I have to use unity version of 2019.1.4. Is there any other alternative to create 3D mesh represents the my 2D Ground Collider?

here is my code:

public class holemesh : MonoBehaviour
{
    public PolygonCollider2D hole2Dcollider;
    public PolygonCollider2D ground2Dcollider;
    public MeshCollider GeneratedMeshCollider;
    public Collider GroundCollider;
    Mesh GeneratedMesh;

    private void OnTriggerEnter(Collider other)
    {
        Physics.IgnoreCollision(other, GroundCollider, true);
        Physics.IgnoreCollision(other, GeneratedMeshCollider, false);
    }

    private void OnTriggerExit(Collider other)
    {
        Physics.IgnoreCollision(other, GroundCollider, false);
        Physics.IgnoreCollision(other, GeneratedMeshCollider, true);
    }

    private void FixedUpdate()
    {
        if(transform.hasChanged == true)
        {
            transform.hasChanged = false;
            hole2Dcollider.transform.position = new Vector2(transform.position.x, transform.position.z);
            MakeHole2d();
            Make3Dmesh();
        }
    }

    private void MakeHole2d()
    {
        Vector2[] PointPositions = hole2Dcollider.GetPath(0);

        for (int i = 0; i < PointPositions.Length; i++)
        {
            PointPositions[i] += (Vector2)hole2Dcollider.transform.position;
        }
        ground2Dcollider.pathCount = 2;
        ground2Dcollider.SetPath(1, PointPositions);
    }
    
    private void Make3Dmesh()
    {
        if(GeneratedMesh != null)
        {
            GeneratedMesh = ground2Dcollider.CreateMesh(true, true); // ---> this part is not working.
            Destroy(GeneratedMesh);
        }
        GeneratedMeshCollider.sharedMesh = GeneratedMesh;
    }
}
question from:https://stackoverflow.com/questions/65647914/is-there-an-alternative-way-to-collider2d-createmesh-in-unity-2019

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...