Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
6
Question by MasterBLB · Jul 05, 2011 at 06:23 PM · meshcreate

Creating a plane mesh directly from code

Hi

As in topic-I'd like to create a small Plane fully programatically.How to do that? I think I have to create a new GameObject then add MeshFilter and MeshRenderer components to it.But what's next?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
12
Best Answer

Answer by zmar0519 · Jul 06, 2011 at 12:01 PM

you could use gameObject.CreatePrimitive(PrimitiveType.Plane);, but that will give you the ten by ten vertice plane we get from the gameObject Menu. I reccomend trying the below code. It will give you a four vertice and two tri plane.

 var size : float;
 function Awake()
 {
      var m : Mesh = new Mesh();
      m.name = "Scripted_Plane_New_Mesh";
      m.vertices = [Vector3(-size, -size, 0.01), Vector3(size, -size, 0.01), Vector3(size, size, 0.01), Vector3(-size, size, 0.01) ];
      m.uv = [Vector2 (0, 0), Vector2 (0, 1), Vector2(1, 1), Vector2 (1, 0)];
      m.triangles = [0, 1, 2, 0, 2, 3];
      m.RecalculateNormals();
      var obj : GameObject = new GameObject("New_Plane_Fom_Script", MeshRenderer, MeshFilter, MeshCollider);
      obj.GetComponent(MeshFilter).mesh = m;
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MasterBLB · Jul 06, 2011 at 04:23 PM 0
Share

I've created something similliar,but there is a problem with visibility :/

Take a look: http://answers.unity3d.com/questions/140070/mesh-created-in-code-is-not-visible.html

avatar image proto_script · Sep 29, 2013 at 09:28 PM 3
Share

Thanks for the code! I created a C# function for $$anonymous$$esh creation (based on the code above), modified to take width and height parameters.

 $$anonymous$$esh Create$$anonymous$$esh(float width, float height)
 {
     $$anonymous$$esh m = new $$anonymous$$esh();
     m.name = "Scripted$$anonymous$$esh";
     m.vertices = new Vector3[] {
         new Vector3(-width, -height, 0.01f),
         new Vector3(width, -height, 0.01f),
         new Vector3(width, height, 0.01f),
         new Vector3(-width, height, 0.01f)
     };
     m.uv = new Vector2[] {
         new Vector2 (0, 0),
         new Vector2 (0, 1),
         new Vector2(1, 1),
         new Vector2 (1, 0)
     };
     m.triangles = new int[] { 0, 1, 2, 0, 2, 3};
     m.RecalculateNormals();
         
     return m;
 }

I call that function from the Awake() function, creating an object to apply the mesh to and setting up the shader and material (to apply a colour):

 void Awake() {
     GameObject plane = new GameObject("Plane");
     $$anonymous$$eshFilter meshFilter = ($$anonymous$$eshFilter)plane.AddComponent(typeof($$anonymous$$eshFilter));
     meshFilter.mesh = Create$$anonymous$$esh(1, 0.2f);
     $$anonymous$$eshRenderer renderer = plane.AddComponent(typeof($$anonymous$$eshRenderer)) as $$anonymous$$eshRenderer;
     renderer.material.shader = Shader.Find ("Particles/Additive");
     Texture2D tex = new Texture2D(1, 1);
     tex.SetPixel(0, 0, Color.green);
     tex.Apply();
     renderer.material.mainTexture = tex;
     renderer.material.color = Color.green;
 }
avatar image
3

Answer by Eric5h5 · Jul 05, 2011 at 06:33 PM

See the CreatePlane script on the wiki.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MasterBLB · Jul 06, 2011 at 09:13 AM 1
Share

Hmmmm and what about struct Plane I've found in code?Have I to make the mesh by assigning those verticles manually,or I just can use that struct?

avatar image Eric5h5 · Jul 06, 2011 at 03:21 PM 1
Share

@$$anonymous$$asterBLB: that's a mathematical plane. Nothing to do with visual planes.

avatar image sosh · Nov 12, 2014 at 02:07 PM 0
Share

There doesn't seem to be a website at that address. Has this moved?

avatar image Bunny83 · Nov 12, 2014 at 02:26 PM 0
Share

@sosh: Yes it has moved as Unity now hosts the wiki on it's own domain. For a long time the old domain was redirected to the new one, but now the domain seems to be outdated. I've fixed the link.

avatar image Rin · Dec 09, 2014 at 02:34 AM 0
Share

This doesn't seem to work I get a C1061 error from it

avatar image
2

Answer by Chris D · Jul 05, 2011 at 06:28 PM

The documentation on the Mesh functions is pretty thorough. Point 1 has a code example to build a mesh from scratch:

 1. Building a mesh from scratch: should always be done in the following order: 1) assign vertices 2) assign triangles

 var newVertices : Vector3[];
 var newUV : Vector2[];
 var newTriangles : int[];
 
 function Start () {
     var mesh : Mesh = new Mesh ();
     GetComponent(MeshFilter).mesh = mesh;
     mesh.vertices = newVertices;
     mesh.uv = newUV;
     mesh.triangles = newTriangles;
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by sysmaya · Jan 30, 2017 at 10:37 AM

 GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
 
 // Position and rotation plane
 plane.transform.position = new Vector3(1f, 2f, 3f);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Any tips for creating grass on mesh(creating gardens) ? 0 Answers

Mesh and material disappear after CreatePrefab() in C# 1 Answer

Generating a cube mesh with two given points 1 Answer

How to connect walls corners 3 Answers

How do you make mesh in Unity? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges