Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[ CLOSED :( ]Trying to make class like Input class, but for makin custom primitives.

Discussion in 'Scripting' started by keenanwoodall, Jul 22, 2014.

  1. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    I'm trying to make a static class that is callable from anywhere. I want it to let me create an object from that class at a specific location. Here is where I'm at
    Code (js):
    1.  
    2. static class Tools
    3. {
    4.     enum Primitive
    5.     {
    6.         Box, Buckyhedron, CapsuleSliced, Cone, ConeRoundedBase, ConeSliced, Cube5x5Beveled,                             CubeMatrixExtrude, CubeTwist
    7.     };
    8.     static function CreatePrimitive (primitiveName : Primitive) : GameObject
    9.     {
    10.         return primitiveName;
    11.     }
    12. }
    I though maybe I could create an enumeration and put all the objects in there. Then I could access the objects through a static function. I know I'm doing plenty wrong. Should I even use enums?

    -Thanks a bunch, Keenan
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    An enum is essentially just a named wrapper around an integer value. There's plenty of language-specific documentation about them if you search around MSDN etc.
     
  3. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Thanks, how would I go about making it so in another code I can type Tools.CreatePrimitive(primitive name, transform). Would i have to make if statements for every object?
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    GameObject.CreatePrimitive is already available to you. Why can't you just use that?
     
  5. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    I'm making an editor extension that comes with a lot of new primitives. I have it set up so you can create them in the editor from the menu, but I also want the objects to be able to be called through scripts
    http://forum.unity3d.com/threads/community-asset-idea.257799/
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Because enum is basically an integer you can use it to index into an array if you want. So you could make an array containing the actual primitive GameObjects and reference into it with enum values cast to integer.

    The more verbose (and uglier) way to do it would be to use a switch or if/else block.
     
  7. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    ok, thanks
     
  8. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Sorry if I'm being nagging..But how would I specifically make it happen. I would create an array in the class with all the objects. Would the array need to be static or public? Then I make an enum and name all of the corresponding "keywords" for each object in the array. Then would I make a public static function? If so, what argument would I pass through? and what would I put inside the function?
    Sorry for asking so many questions! I'm really excited to figure this out :)
     
  9. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Also, I know I can access the primitives through Resources, which is the folder I have them in. How would I set up a for loop statement that adds all of the objects in the primitive folder to the array, and then possibly the enum? I would love to know because I have A LOT of primitives and it took me near a whole day to type out each primitives' @MenuItem and static function, even with copying and pasting!
     
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    If your class is static then everything in it needs to be static.

    Assuming you want it to work like GameObject.CreatePrimitive then your method should take an enum.
     
  11. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    ok thanks again, what would I feed through my function
    Code (csharp):
    1.  
    2. static function CreatePrimitive (?)
    3. {
    4.      ?
    5. }
     
  12. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    In your static method check if the array has elements in it and if it doesn't then load them from Resources.
     
  13. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    God I feel dumb, So in my CreatePrimitive function, between the brackets, I'll put
    Code (js):
    1.  
    2. for (obj : GameObject in Resources.Load("PrimitiveFolder"))
    3. {
    4. //And then here I would add that obj to the objArray that I would create right under the Tools class? I don't know how to do this either :(
    5. }
     
  14. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Then you should take a step back and do some basic programming tutorials.
     
  15. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    ok, sorry for wasting your time man
     
  16. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You're not wasting my time - or anyone else's time. I'm simply saying you'll be better served by having a good grasp of the conceptual foundations of programming.
     
  17. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Yeah, I mean I'm really solid at most basic stuff, and then a couple intermediate. I learn whatever comes to me easiest, first. I learned how to communicate between objects way before I understood for loops! the are both pretty basic, but loops are absolute beginner concepts. I am not sure why my brain works that way!