Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
0
Question by zerophase · Jan 08, 2015 at 12:28 PM · c#vector3vector2genericspolicy

Writing Policy class for handling Vector2s and 3s.

I'm finding I have to write a lot of similar code in certain situations for Vector2s and 3s. So, I figured write a generic class that takes a VectorPolicy and does calculations with the appropriate type. The only issue is Vector2s and 3s don't have interfaces backing them. So, I decided to make a policy class that would call the appropriate methods on each. Like so:

 public interface IVectorPolicy<T>
 {
      T Add(T a, T b);
      float Dot(T lhs, T rhs);
 }

That I'd use like so:

 public class Test<T, G> where T : IVectorPolicy, new()
 {
     readonly T vectorPolicy;
 
     public Test()
     {
     vectorPolicy = new T();
     }
 
     public Foo(G vector1, G vector2)
     {
        G g = vectorPolicy.Add(vector1, vector2);
        float dotProduct = vectorPolicy.Dot(g, vector1);
     }
 }

But this causes other issues with G not being known at compile time. As far as I'm aware the compiler cannot figure out the Type of a Generic Type being passed to another Generic Type at compile time. Anyone have any idea how I could work around this?

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
0
Best Answer

Answer by Owen-Reynolds · Jan 08, 2015 at 06:01 PM

Think you'd get better results in a pure C# forum. The only Unity thing in the Q is Vector2/3, and that should be easily understandable as "2 similar structs" -- and I think most people will recognize them as the more common float2 and float3 types.

My solution was never to use Vector2's. I had some data which was really a worldGrid xz position, so stored it as a Vector2. Figured I'd convert it to (x,0,z) format and back as needed. Was no end of trouble keeping them straight, esp. as V2s cropped up elsewhere that really were xy.

I just put that xz Vector2 attempt under "premature optimization." Of course, I do use V2's for purely grid-based stuff, when I know I'll have one well-marked conversion to and from world coords.

Comment
Add comment · Show 1 · 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 zerophase · Jan 10, 2015 at 10:52 AM 0
Share

I've looked into the c# answers on this, and it sounds like it requires heavy use of reflection. Thanks for the help.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Are Unity distances that small? 1 Answer

C# Convert Vector3[] to Vector2[] 3 Answers

Create custom constructors for certain Value types/Objects (Vector3, Transform, etc...) 2 Answers

how to find a point(vector2) between two points(Vector2) of a line (line renderer) 2 Answers

Transfer values from C# to Javascript 1 Answer


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