Jump to content

AffinityUI, an easier way to write Unity GUIs


nalitia

Recommended Posts

Hey all, this is a library I wrote to aid in the development of Unity GUIs (the legacy kind that KSP still uses). I started on it years ago, but it fell by the wayside as work became more important. Recently I decided to finish it and release it for the KSP community.

When I first started using Unity about 5 years ago, I couldn't stand the way the GUI system was designed. I was used to the declarative model and rich data binding capabilities of frameworks like WPF, so I set out to design my own in Unity. This is the result.

Features

  • Declarative UI – specify what you want your GUI to do, the framework takes care of the rest. No minutia of plumbing values in and out of GUILayout calls.
  • Rich data binding without a big performance impact. Unlike WPF, there is no reflection, only function calls.
  • Skin and style support
  • Full set of controls wrapping existing Unity controls, plus new controls such as TabControl
  • Floating tool tip rendering
  • Interoperable with any existing GUI code
  • No fancy confusing magic, easy to read code

It's really, really easy to build very complex GUIs and new controls with this library. I put together a short example of the basic capabilities:

It's pretty flexible and powerful, and there's still a lot I could add. I'm unsure, however, of the future value of this project given that KSP will be migrating to Unity 5. But maybe it will be useful for legacy projects.

If you think this is useful, please let me know! I'll continue to work on this project and support it if people are interested. There are more features that could be added, as well as documentation and tests.

Project on GitHub

Releases, example project, and information on how to build can be found here:

https://github.com/mattolenik/AffinityUI

Link to comment
Share on other sites

Wow this looks cool! I will tinker with this. I've had a few mod ideas but the GUI development process is intimidating. Is this intended to work as an API like blizzy's toolbar (looks REALLY close already) and what license do you plan?

Also, I'm thinking you should add your /lib directory (with the dependence .dlls) on github to .gitignore and remove them from github. I could be wrong, but I recall that being an issue in the past.

Summary: I think this is useful!

Link to comment
Share on other sites

It's not an API like blizzy's toolbar, it's just an abstraction over the Unity GUI API. You can use it for any game, there isn't even anything KSP specific.

Also, the binaries in lib are actually symlinks, they're not really there. I wasn't sure if they were redistributable.

Thanks for asking about the license. It's supposed to be MIT, I'd just forgotten to add it to the repo.

Link to comment
Share on other sites

Wow, this is exactly what I was hoping someone would do for this kind of thing. I have absolutely no clue about how to write a GUI, nor the patience to figure it out. Besides, I'd probably just get frustrated and pull all my hair out. I'm not old enough for hair loss... only greys.

Link to comment
Share on other sites

It's a much better GUI system, so I don't mind. I may look into writing something similar for the new UI. There are other Unity projects that already do data binding, but the only ones I've seen either cost money, or use heavyweight XML markup and a lot of reflection.

I think the legacy UI still works in the newer Unity, but is considered deprecated. Maybe still useful for mods that don't want to do a complete rewrite? I don't know much about it, and I don't know how things will work w.r.t KSP. I'll have to look into it.

Link to comment
Share on other sites

The Unity 5 docs have all of the old GUI stuff listed under legacy, so I imagine it's all still supported. And as far as I know all of the new UI stuff came in Unity 4.6, which KSP already uses, so old GUI code will probably continue working for the foreseeable future.

I have a very simple UI mod that I came up with from something I saw in the suggestions forum (a row of icons in the space center and keyboard shortcuts for highlighting and entering different buildings). After getting frustrated trying to find instances of the Space Center Buildings I realized I should try to use the new UI system. I figure it would serve as a simple exercise in the new UI coding. I'll make sure to document everything well if I can get it working.

Link to comment
Share on other sites

Yep, I have the same one bookmarked :wink:

So far that's the only good scripting documentation I've seen.

There is a pretty comprehensive Unity tutorial that goes over the types of objects in the new UI. It's all done in the editor, but it still gives a good idea of what's possible.

Link to comment
Share on other sites

That's very useful, thank you! My first glance at the new UI showed it to be done in the editor, which would be a problem for KSP or other games where we're doing work with mods. Similarly all the projects in the Unity asset store are not very useful to KSP modders.

Link to comment
Share on other sites

Yes, all their tutorials are in the editor but the system always was scriptable and the source are even open.

With some work something that spew code from the editor could be done too but it may be a bit too

As for usefull code, here is the other links I had

unity-ui-extensions repo with some higher level controls and extensions

The thread where those controls were shared (may have a few not in the repo)

Link to comment
Share on other sites

So I managed [thread=122744]this[/thread] using the new UI.

6dbXGzN.png

It's basically just ten buttons laid out along the bottom, a text label is added above and to the left of each button. Everything is manually positioned, I think there is some kind of layout system, but I don't know how it works. I extended the base UI.Button class to add things like right-click and mouse-over detection (which seem to be half-way implemented already), but otherwise I'm just using basic UI objects.

The new UI system is begging for some kind of abstraction like you have here for the legacy GUI stuff. Creating each object is a bit tedious and there are several things I can't get to work. I don't know how to assign a background image to the buttons for instance, each one only seems to accept one image object. And I can't get the UI elements on top of that bottom bar.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...