Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

c# - How to declare enum with Vector3 Directions?

In an example found in W3Schools, I saw that you can give a custom value to an Enum, like this:

enum Months
{
  January,    // 0
  February,   // 1
  March=6,    // 6
  April,      // 7
  May,        // 8
  June,       // 9
  July        // 10
}

For my game, I often have to check the neighbors of a certain position (UP, DOWN, LEFT, RIGHT), so I made this array:

Vector3Int[] neighborOffsets =
        {
            new Vector3Int(0, -1, 0), // Bottom
            new Vector3Int(-1, 0, 0), // Left 
            new Vector3Int(0, 1, 0), // Top
            new Vector3Int(1, 0, 0) // Right
        };

However, if I require a specific and known value, I have to remember which position to access by it's index. So if I explicitly need the TOP direction, I'd have to write: neighborOffsets[2].

Is there an easier way to do this with enums? Something like this:

public enum Directions
{
   BOTTOM = new Vector3Int(0, -1, 0),
   LEFT = new Vector3Int(-1, 0, 0), 
   TOP = new Vector3Int(0, 1, 0), 
   RIGHT = new Vector3Int(1, 0, 0) 
}

So then if I need the UP direction, I'd just write Directions.TOP?

question from:https://stackoverflow.com/questions/65901653/how-to-declare-enum-with-vector3-directions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Answered by @Charleh in the comment:

This already exists in unity: Vector3Int so just copy this; is it just the syntax that you are looking for? Use a static property, e.g. public static Vector3Int bottom = new Vector3Int(0, -1, 0), then access by type name.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...