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
0
Question by Ferdil · Jun 28, 2010 at 01:14 PM · inputupdateclassstaticinputmanager

Update() in non-component script?

I'm building a custom input manager since Unity's default one does not satisfy me. I built a custom static class in C# to access all of the set actions, but I need it to check the devices for input constantly.

How to get a function that is executed once per frame in a static class?

Alternatively, how to get a MonoBehaviour-derived static class to execute an update function withot having been linked to a GameObject?

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

3 Replies

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

Answer by Mike 3 · Jun 28, 2010 at 01:25 PM

You can't - at some point, you have to have a monobehaviour in the scene to propagate the event

The easiest way is to create a simple script which calls an event in your static class, which other classes register their delegates to

e.g.

void Update() { YourStaticClass.Update(); }

//in YourStaticClass:

delegate void UpdatingEventHandler(); public event UpdatingEventHandler Updating;

void Update() { if (Updating != null) Updating(); }

//in a class wanting to listen to the event:

YourStaticClass.Updating += OnUpdate; //this goes anywhere, a constructor maybe

void OnUpdate() { //your code }

Comment
Add comment · Show 4 · 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 Ferdil · Jun 28, 2010 at 01:51 PM 0
Share

But the $$anonymous$$onoBehaviour has to be linked to some object in the scene?

avatar image Cyclops · Jun 28, 2010 at 01:58 PM 0
Share

@Ferdil, it can be an Empty GameObject, which won't be visible. Or anything else handy, like a Camera.

avatar image Ferdil · Jun 28, 2010 at 02:11 PM 0
Share

But why use events/delegates? Wouldn't a simple function call be enough?

avatar image Mike 3 · Jun 28, 2010 at 03:52 PM 0
Share

Then you'd have to call every single function which needed to be drawn. Say you have 50 classes which want the GUI event - you'd have to add all of them into the code. With the event, you just register a delegate from the class which wants it and you're done.

avatar image
5

Answer by vargonian · Dec 28, 2013 at 09:26 AM

Another option is that your custom class an implement an interface that you define called IUpdateable (or something like that) which contains a single method, Update(float dt). You can then keep track of these IUpdateables in whatever way suits you and have their Update methods called during a MonoBehavior's Update in your scene. So, if you have a CoreGameBehavior script or something, its Update method might look like this:

 foreach (IUpdateable updateable in this.updateables)
 {
     updateable.Update(Time.smoothDeltaTime);
 }

This has a benefit in that your IUpdateable interface need not depend on any Unity libraries.

This is of course a simple version; your needs may vary.

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
0

Answer by Ward Dewaele · Jul 09, 2010 at 02:35 AM

i was doing the same as you - feel free to check my custom input manager out. I released it for free.

Currently it can do this : - read input from joystick buttons and (up to 4) axis - read input from mouse buttons and axis - read input from all keys from the keyboard - save the configuration to PlayerPrefs (and ofcourse load them back) - reset to default keys (which u can setup in the inspector)

Visually im not an artist so u have to pimp it up yourself. This will work in both standalone and webbuilds. Hope this satisfy you - the link is HERE

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

1 Person is following this question.

avatar image

Related Questions

Supplying Input from Update To FixedUpdate 1 Answer

Exactly when are inputs updated? 1 Answer

Call to "Input" into a static class => null ref 1 Answer

Can a static class have an Update() function which Unity calls automatically? 1 Answer

Mouse Input for RayCast - Update or FixedUpdate 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