Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
0
Question by iron_attorney · Jul 13, 2015 at 04:44 PM · c#functionlistsfunction callpassing

function that returns multiple variables (in this case, lists)

Hi there, is it possible to make a function that returns multiple variables? More specifically for my example, multiple lists. My application is procedural mesh generation, in which I am calling my own mesh generators and mesh effects that I have coded in static scripts. Previously, I have been setting up a gameObject in one script, and calling a mesh generator in another, to which I pass a mesh. Then I pass the new mesh to several mesh effects one at a time which each return the new mesh before being passed to the next. I feel there should be a quicker way than this (while still keeping my code modular), so I am now trying to pass each generator and effect function a series of lists (for verts, tris, uvs and normals) instead of the mesh. When I pass the mesh to a function, it is passed as a reference and therefor doesn't need return to continue work on it. When I pass the lists however, they are not referenced, and I therefor need a way to return the 4 lists. Here is an attempt of mine at returning the lists:

The function that sets up the gameObject:

 void TerrainGenerator (int xQ, int yQ, float w, float d) {
 
         List<Vector3> verts = new List<Vector3> ();
         List<int> tris = new List<int> ();
         List<Vector2> uv = new List<Vector2> ();
         List<Vector3> norms = new List<Vector3> ();
 
         GameObject quad = new GameObject ("Quad", typeof(MeshFilter), typeof(MeshRenderer));
         
         Mesh mesh = new Mesh ();
         
         MeshGenerators.CreatePlane (verts, tris, uv, norms, xQ, yQ, w, d);
 
         mesh.vertices = verts.ToArray();
         mesh.triangles = tris.ToArray();
         mesh.uv = uv.ToArray();
         mesh.normals = norms.ToArray();
         
         MeshFilter mesh_filter = quad.GetComponent<MeshFilter> ();
         mesh_filter.mesh = mesh;
         
         MeshRenderer mesh_renderer = quad.GetComponent<MeshRenderer> ();
         mesh_renderer.material = quadMaterial;
     }

and the generator function that is called by the above function:

 public static void CreatePlane (List<Vector3> vertices, List<int> triangles, List<Vector2> uv, List<Vector3> normals, int xQuads, int yQuads, float quadWidth, float quadDepth) {
         
         for (int y = 0; y <= yQuads; y++) {
             for (int x = 0; x <= xQuads; x++) {
                 
                 vertices.Add (new Vector3 (x * quadWidth, y * quadDepth, 0));
                 normals.Add (Vector3.up);
                 uv.Add (new Vector2 ( x / (xQuads), y / (yQuads) ));
             }
         }
         
         for (int y = 0; y < yQuads; y++) {
             for (int x = 0; x < xQuads; x++) {
                 
                 triangles.Add ( 0 + x + (xQuads + 1) * y ); 
                 triangles.Add ( 1 + x + (xQuads + 1) * y );
                 triangles.Add ( 1 + x + (xQuads + 1) * (y + 1) );
                 triangles.Add ( 0 + x + (xQuads + 1) * (y + 1) );
             }
         }
         
         return vertices;
         return triangles;
         return uv;
         return normals;
     }

I'm not sure it's even possible, but any pointers would be greatly appreciated. Also, if you don't think there would be any advantage to passing the lists over passing the mesh itself, do tell!

Cheers in advance,

Pete

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

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

Answer by Dave-Carlile · Jul 13, 2015 at 04:57 PM

When passing a List (or any class) as a parameter to a function, it is always passing a reference to the list. So anything you do to the list contents (e.g. adding a vertex) is being done to the original because they're pointing to the same thing. The only time you would need to pass back the list reference itself is if you're creating a new list instance, and it doesn't look like you're doing that.

That said, if you need to deal in the list references themselves, pass them as ref parameters.

 void DoSomething(ref List<Vector3> vectors)
 {
    vectors = new Vectors();  <-- the caller will get a new instance
 }


 List<Vector3> v = null;
 DoSomething(ref v);     <-- v will come back with a new list instance


But again, I don't think you need to do that in your case. You're creating the lists before you call the functions and the functions are just altering the contents.

And unless you're ever going to be creating non-Unity meshes, you may as well just pass the Mesh instance around instead of the individual properties.

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 iron_attorney · Jul 13, 2015 at 08:48 PM 0
Share

I thought I'd tested to see if altering the list without any kind of return code would affect it, maybe I missed something though, I'll report back on that.

But you reckon I'd be just as well off passing the mesh ins$$anonymous$$d? Well, that is much easier to code! I'll still test the lists in order to wrap this question up (might come in handy for something else). I might even do a little performance test to see if there is a notable difference.

Cheers for your help Dave!

Pete

avatar image iron_attorney · Jul 13, 2015 at 09:04 PM 0
Share

Ok you're right, it does work without any kind of out/ref/return. I think I'd bodged up the triangles on that particular attempt (I'd added 4 per quad ins$$anonymous$$d of 6... doh!) and mistook it for the lists not working.

If I get a chance to do a performance test, I'll report back again.

Cheers again!

Pete

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Function being called on multiple objects when just one is clicked 1 Answer

List.add() not working when function called by 2nd script 1 Answer

is it possible to pass functions through functions? 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