Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Oct 10, 2016 at 07:33 AM by RangerDog for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by RangerDog · Oct 07, 2016 at 11:45 AM · rectangle

How to draw a rectangle using scripts

So basicly, i want to draw a rectangle with lines. I have 2 sets of objects being spawned, and i want a line around them, but much rather a big rectangle around the area they can spawn in.

Right now i have this:

     void DrawLine(Vector3 start, Vector3 end, float duration = 0.2f)
     {
         GameObject myLine = new GameObject();
         myLine.transform.position = start;
         myLine.AddComponent<LineRenderer>();
         LineRenderer lr = myLine.GetComponent<LineRenderer>();
         lr.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
         lr.SetWidth(0.1f, 0.1f);
         lr.SetPosition(0, start);
         lr.SetPosition(1, end);
     }


Which works ok, but for 1 rectangle you already need 4 lines of code, and you cant edit it that easliy, so my question is is there any way to do this but then in a rectangle?

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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Zodiarc · Oct 07, 2016 at 11:48 AM

Look here http://answers.unity3d.com/questions/1241034/procedural-mesh-has-weird-offset.html

Comment
Add comment · Show 13 · 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 RangerDog · Oct 07, 2016 at 12:05 PM 0
Share

Well it worked, i only don'r really get what it does as in, the mesh is a cube, but it sill spawns a triangle :S, Trying to figure it out, but if you could explain it would be awesome :3

(So mostly, how would you for example spawn 2 on different locations, since when you spawn them in you don't give a location.

How do you change the mesh, size etc?

:S

avatar image Zodiarc RangerDog · Oct 07, 2016 at 12:18 PM 0
Share

Basically the vertices array defines the point cloud of which the mesh consists, the uv array the uv coordiantes (but I don't know how it works for more complicated meshes than a square, need to figure it out) and tris defines the indexes which build the trianglular faces of the mesh. Every set of 3 integers in that array defines a triangle.

 float scale = 1.0f
 
 Vector3[] vertices = new Vector3[] 
          {
               new Vector3(-scale / 2, 0, scale / 2),
               new Vector3(scale / 2, 0, scale / 2),
               new Vector3(-scale / 2, 0, -scale / 2),
               new Vector3(scale / 2, 0, -scale / 2)
           };
  
          Vector2[] uv = new Vector2[]
          {
              new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0),new Vector2(0, 0)
          };
  
          int[] tris = new int[]
          {
              2,1,0,1,3
          };
          Debug.Log(this.tile$$anonymous$$esh);
          this.tile$$anonymous$$esh.vertices = vertices;
          
          this.tile$$anonymous$$esh.uv = uv;
          this.tile$$anonymous$$esh.triangles = tris;
          this.GetComponent<$$anonymous$$eshFilter>().mesh = this.tile$$anonymous$$esh;
          this.GetComponent<$$anonymous$$eshRenderer>().material = this.material;

Should build a square. By changing the scale variable you can change the size. Also you need to take care of the normals. If some part isn't displayed just change the integers in the tris array around. There's also another way to recalculate normals but I don't know how it works.

avatar image RangerDog Zodiarc · Oct 07, 2016 at 12:48 PM 0
Share

Throws out the errors: Assets/LineController.cs(31,42): error CS1526: A new expression requires () or [] after type

And

Assets/LineController.cs(31,52): error CS1525: Unexpected symbol )', expecting ,', or `}'

Basicly the Vector3 part, its in C# (Previous part worked tough)

Show more comments
Show more comments
avatar image Zodiarc RangerDog · Oct 07, 2016 at 01:09 PM 0
Share

Ok so change the tris array to

2,1,0,0,1,3

avatar image RangerDog Zodiarc · Oct 07, 2016 at 01:16 PM 0
Share

Error is gone, still a triangle tough.

Show more comments
avatar image RangerDog · Oct 07, 2016 at 01:26 PM 0
Share

That makes 3 triangles and it's upside down (Sorry, never worked with tris before, also googling it XD)

avatar image Zodiarc RangerDog · Oct 07, 2016 at 01:30 PM 0
Share

Weird. It should create 2. Can you post a screenshot?

The upside down problem can be solved by inverting the tris array to

0,1,3,0,1,2

If this doesn't work in a few hours I'll be able to look it up because I have this up and running in my project.

avatar image Zodiarc Zodiarc · Oct 07, 2016 at 01:35 PM 0
Share

Forgot I have it on my gitlab:

     public class Adaptive$$anonymous$$eshTile : AbstractTileController
     {
         public $$anonymous$$aterial material;
         private $$anonymous$$esh tile$$anonymous$$esh;
 
         // Use this for initialization
         public void Start()
         {
             this.tile$$anonymous$$esh = new $$anonymous$$esh();
             Vector3[] vertices = new Vector3[]
             {
                 new Vector3(-scale / 2, 0,  -scale / 2),
                 new Vector3(scale / 2, 0, - scale / 2),
                 new Vector3( -scale / 2, 0,  scale / 2),
                 new Vector3( scale / 2, 0,  scale / 2)  
             };
 
             Vector2[] uv = new Vector2[]
             {
             new Vector2(0 ,0), new Vector2(0, 1), new Vector2(1, 0), new Vector2(1, 1)
             };
 
             int[] tris = new int[]
             {
             2,1,0,1,2,3
             };
             this.tile$$anonymous$$esh.vertices = vertices;
 
             this.tile$$anonymous$$esh.uv = uv;
             this.tile$$anonymous$$esh.triangles = tris;
             this.GetComponent<$$anonymous$$eshFilter>().mesh = this.tile$$anonymous$$esh;
             this.GetComponent<$$anonymous$$eshRenderer>().material = this.material;
         }
     }
Show more comments
avatar image RangerDog · Oct 10, 2016 at 07:13 AM 0
Share

Solved it by turning the GameObject 180 degrees, not perfect but ah well. Thanks!

Follow this Question

Answers Answers and Comments

57 People are following this question.

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

Related Questions

How to select a unit in my RTS game? 0 Answers

RectTransformUtility.ScreenPointToLocalPointInRectangle does not work correctly? 0 Answers

Button which I can choose the exact looks on 1 Answer

How to not scale my Texture? 1 Answer

Using 2d image for dragging? 0 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