Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by combatdave · Apr 24, 2012 at 09:18 AM · inspectorsystemtimespan

Custom inspector for System class (TimeSpan)

I'm making use of System.TimeSpan in a couple of places, and I want these to be editable in the inspector. What's the best way of going about this? I tried the following but it doesn't work as UnityEngine.Object cannot be converted to System.Timespan...

 [CustomEditor(typeof(TimeSpan))]
 public class TimeSpanInspector : Editor
 {
     private TimeSpan _ts;

     public void OnEnable()
     {
         _ts = (TimeSpan)target;
     }

     public override void OnInspectorGUI()
     {
         GUILayout.BeginHorizontal();
         int hours = EditorGUILayout.IntField(_ts.Hours, GUILayout.Width(50));
         GUILayout.EndHorizontal();

         _ts = new TimeSpan(hours, 0, 0);
     }
 }
Comment
Add comment · Show 1
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 John-LZG · Feb 27, 2014 at 07:50 PM 0
Share

I'm also looking to do this, though I'm trying to use the CustomDrawer, as that'll make timespans show up no matter what they're in... but yeah, still not working.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Jamora · Feb 27, 2014 at 09:26 PM

The easiest way to accomplish this would be to write a MonoBehaviour wrapper for TimeSpan and then have references to that wrapper. This does impose some limitations, namely, that you need to attach your TimeSpan-wrapper to a GameObject. To inspect this object, you firstly need a custom inspector (or a propery drawer). Secondly you need to use the getters and setters in the wrapper instead of directly accessing the TimeSpan when inspecting.

For those who do not know what a wrapper is, it's basically a class that is 'wrapped' around another class, providing suitable implementations for the current platform. The TimeSpan wrapper could look something like this:

 public class TimeSpanWrapper : MonoBehavior{
     private TimeSpan timeSpan;

     //use this to create new TimeSpanWrappers
     public static TimeSpanWrapper Create(GameObject go, int hours, int minutes, int seconds){
         TimeSpanWrapper tsw = go.AddComponent<TimeSpanWrapper>();
         tsw.Init(hours, minutes, seconds);
         return tsw;
     }
 
      public void Init(int hours, int minutes, int seconds){
          timeSpan = new TimeSpan(hours, minutes, seconds);
          //this is actually the constructor where we set
          //the values for the private TimeSpan.
      }
 
      //Here goes methods you need from TimeSpan. If
      //TimeSpan had a GetHours method, we would create a
      //method as follows:
      
      public int GetHours(){ return TimeSpan.GetHours(); }
 }
Comment
Add comment · 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
1

Answer by ben-tmg · Feb 27, 2014 at 09:35 PM

Everything in the inspector must derive from UNityEngine.Object (that is what the error means). To get around this you'll need to create a new class that has a TimeSpan variable on it.

ie

 public class UnityTimeSpan : MonoBehaviour
 {
     public TimeSpan span;
 }

Then you'll have to use UnityTimeSpan instead of TimeSpan and you can create an editor for UnityTimeSpan.

Comment
Add comment · 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

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

7 People are following this question.

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

Related Questions

Accessing local system ( File Browser ) 2 Answers

Help, getting ambiguous reference error 1 Answer

Help, getting ambiguous reference error 1 Answer

See if a time span is bigger than X 4 Answers

Is there any class, function, or variable available from a unity shader to get either system time or real time. 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