I am trying to render a custom mesh in the new GUI system. I have a UI Canvas and then a small hierarchy of game objects under them is a game object with a custom mesh. I can’t seem to get it to render…
I have made sure that my hierarchy are all using the “UI” layer and tried to add Canvas Renders at each level, including the custom mesh.
HI, you can add a canvas renderer and manually set the vertices. We will be adding support for making any ‘renderer’ work with the UI in a future release.
Hey etoreo, would it be possible to post a small code example of how you’re doing this (“manually set the vertices”)? I’m not quite sure what Tim means. Thanks!
@etoreo_1 in your script you do: pCanvasRenderer.SetVertices(pVertexList); what does your pVertexList look like? And where are you setting the uvs? Thanks!
@phil-Unity & @Tim-C Thanks for sharing some insight into Unity’s plans. I too would love to have the CanvasRender accept triangles or to just use a MeshRenderer is another way to do it.
Maybe you could answer a question I have: Why does “CanvasRenderer” appear to inherits from “Component” and not “Renderer”? I was working on a custom UI Graphic type and wanted to set (2) textures for (2) [PreRenderData] properties in my custom UI shader. I was surprised to find that I could not use SetPropertyBlock and that the SetMateral(Material, Texture) will only change the _MainTex.
UIVertex vert;
Mesh mesh;
List<UIVertex> pVertexList = new List<UIVertex>();
int[] indices = mesh.GetIndices();
for (int i=0; i<indices.Length; i+=3) {
for (int p=0; p<3; p++) {
vert = new UIVertex();
vert.position = mesh.vertices[indices[i+p]];
vert.normal = mesh.normals[indices[i+p]];
vert.uv0 = mesh.uv0[indices[i+p]];
//do the same for tangent, uv1, and color if you need to.
pVertexList.Add(vert);
}
//This just adds the last vertex twice to fit the quad format.
pVertexList.Add(vert);
}
@phil-Unity & @Tim-C I work on a custom system too and this is quite painfull to not inherits from Renderer.
My system was using the enabled/disabled system from renderer and with the CanvasRenderer it’s impossible ?
Some people says SetAlpha to 0 with a CanvasGroup but it’s not really a good solution cause the mesh are always rendered even if the user can’t see them.
Others people says do a SetActive to false but it’s not a viable solution for me too cause if the object has a script that need to be used for some reason the Update state will not be displayed ( except if i’m wrong on this part ? )
Why this “CanvasRenderer” that you called as Renderer doesn’t follow the Renderer functionalities ?
@phil-Unity ah ha! that would explain it. Thanks for the info.
Although, maybe it shouldn’t be called a “Renderer” then. Maybe “CanvasBatcher” or something. Probably too late for that; that ship has sailed. Alternatively, Unity should work to pass-through or emulate as much functionally from the mother MeshRenderer to the CanvasRenderer. ie. Setting triangle indices or just accept a mesh, enabled/disabled individual graphics, Setting Property Blocks beyond just the MainTex, etc.
Also, just a quick side note. UIVertex doesn’t seem to be set as serializable. That might work for you all but it would be great if I could save and reuse my List.
Sorry it has taken so long to respond. My vertext list looks like this… but I should note that something about my code is not working in 4.6.3. Trying to work that out now.
Rect pDrawArea = DrawArea;
Vector3 a = new Vector3(pDrawArea.xMin, pDrawArea.yMax, 0);
Vector3 b = new Vector3(pDrawArea.xMax, pDrawArea.yMax, 0);
Vector3 c = new Vector3(pDrawArea.xMin, pDrawArea.yMin, 0);
Vector3 d = new Vector3(pDrawArea.xMax, pDrawArea.yMin, 0);
UIVertex pUIVertex = new UIVertex();
pUIVertex.position = c;
pUIVertex.uv0 = new Vector2(1, 1);
pList.Add(pUIVertex);
pUIVertex.position = a;
pUIVertex.uv0 = new Vector2(0, 0);
pList.Add(pUIVertex);
pUIVertex.position = b;
pUIVertex.uv0 = new Vector2(0, 1);
pList.Add(pUIVertex);
pUIVertex.position = d;
pUIVertex.uv0 = new Vector2(1, 0);
pList.Add(pUIVertex);
@etoreo_1 thanks for posting your code. I can render the mesh but it is all black no matter what material I use. Any ideas what might be causing this? Thanks!