Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

unity3d - Storing informations on different object ( Unity C#)

I'm trying to store informations about a Cube that is build out of 6 separate faces in a different GameObject. so I can have multiple versions of this cube, all with different parameters like color, size, resolution of each face etc. It is important that in the end I have n versions or copies of that one Object with totaly different values. The faces are generated as child Objects and just contains a Mesh Renderer and Mesh Filter component.

My first attempt was to make a prefab out of the existing cubeObject and then instantiate this prefab somewhere else (like on a placeholder transform in a different scene). When I create a prefab out of the existing cube the MeshFilter of each face is empty/ null.

another try was to think about saving all parameters somehow and then applying them to another cube. but I'm not sure how to do this in a proper way? maybe Making the hole thing a scriptable object OR I don't know.

The other core issue is if i dublicate the cube and then change the value of one of them ALL cubes get changed because of the way the cube is generated. Any suggestiones how to solve this would help a ton!

FACES.cs:

    private Mesh mesh;
    private int resolution;
    private Vector3 localUp;

    private Vector3 axisA, axisB;

    public Face(Mesh mesh, int resolution, Vector3 localUp)
    {
        this.mesh = mesh;
        this.resolution = resolution;
        this.localUp = localUp;

        axisA = new Vector3(localUp.y, localUp.z, localUp.x);
        axisB = Vector3.Cross(localUp, axisA);
    }

    public void ConstructMesh()
    {
        Vector3[] vertices = new Vector3[resolution * resolution];
        // getting nb of vertices; nmb of faces = (r-1)^2; *2 for triangles; *3 to get all vertices for every triangle 
        int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6];

        int triangleIndex = 0;

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                // index of vertices
                int i = x + y * resolution;
                Vector2 percent = new Vector2(x, y) / (resolution - 1);
                Vector3 pointOnUnitCube = localUp + (percent.x - 0.5f) * 2 * axisA + (percent.y - 0.5f) * 2 * axisB;
                // get vertices same distance to center of cube
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
                // calculate elevation and set them to the vertices
                vertices[i] = pointOnUnitSphere;

                // check if index is not outside the face
                if (x != resolution - 1 && y != resolution - 1)
                {
                    // first triangle, clockwise
                    triangles[triangleIndex] = i;
                    triangles[triangleIndex + 1] = i + resolution + 1;
                    triangles[triangleIndex + 2] = i + resolution;
                    // second triangle, clockwise
                    triangles[triangleIndex + 3] = i;
                    triangles[triangleIndex + 4] = i + 1;
                    triangles[triangleIndex + 5] = i + resolution + 1;
                    // update triangleIndex by 6 because we created 6 triangle
                    triangleIndex += 6;
                }
            }
        }
        mesh.Clear();
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();

    }

CUBE.CS:

public int resolution = 5;

[SerializeField, HideInInspector]
private MeshFilter[] meshFilters;
private Face[] faces;


void Initialize()
{
    if (meshFilters == null || meshFilters.Length == 0)
    {
        meshFilters = new MeshFilter[6];
    }        
    faces = new Face[6];

    // all cardinal directions
    Vector3[] directions = { Vector3.up, Vector3.down, Vector3.left, Vector3.right, Vector3.forward, Vector3.back };


    for (int i = 0; i < 6; i++)
    {
        if(meshFilters[i] == null)
        {
            GameObject meshObj = new GameObject("mesh");
            meshObj.transform.parent = transform;

            meshObj.AddComponent<MeshRenderer>().sharedMaterial = new Material(Shader.Find("Universal Render Pipeline/Lit"));
            meshFilters[i] = meshObj.AddComponent<MeshFilter>();
            meshFilters[i].sharedMesh = new Mesh();                               
        }

        // generate Faces
        faces[i] = new Face(meshFilters[i].sharedMesh, resolution, directions[i]);
    }
}

void GenerateMesh()
{
    foreach (Face face in faces)
    {
        face.ConstructMesh();
    }
}

private void OnValidate()
{
    Initialize();
    GenerateMesh();
}

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

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...