Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Variables created from editor and saved

Discussion in 'Immediate Mode GUI (IMGUI)' started by Athomield3D, Nov 22, 2015.

  1. Athomield3D

    Athomield3D

    Joined:
    Aug 29, 2012
    Posts:
    193
    Hi, I'm trying to make a window to create variables to be used at run-time.

    Example
    Purely create it from editor:



    Then Later on be able to use it in game like
    Code (CSharp):
    1. float speed = SomeRandomClass.AccessProperty("Speed");
    The biggest issue I'm facing right now is how to store the contents of the table.
    Any help is appreciated.
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    One issue with your example API is how would AccessProperty know what is the correct return type?
    From the screenshot, it seems that you're able to set up a number of different return values. This means that AccessProperty should be able to support these.

    this will not work, since you cannot define method overloads that differ only by the return type, so you cannot define multiple AccessProperty methods with different return types (e.g: float, int, string, etc).

    To implement this, I can think of a few different solutions:

    1. Source code generation. Think of something like T4 (read more here: https://msdn.microsoft.com/en-us/library/bb126445.aspx). Once you define all the fields, you can run it through a processor that will generate a code file with all the different data types exposed as fields / properties. Each with its own correct type.

    2. Code generation - you can use Mono.Cecil (http://www.mono-project.com/docs/tools+libraries/libraries/Mono.Cecil/). You can generate a new assembly on the fly and store that into the project. This is the same as #1, only working at the IL level, generating a .DLL at the end of the process.

    3. Define a different API than the one you suggested, and use some sort of data "storage" (class? scriptableobject?).
    For example:

    SomeRandomClass.GetFloat(string name)
    SomeRandomClass.GetInt(string name)

    Under the hood, you can use a dictionary (or multiple dictionaries - one for each data type) and return the value from that dictionary.

    There are probably many different ways to solve this problem. These are only a few suggestions that can give you some food for thought on how to implement this.
     
    Athomield3D likes this.
  3. Athomield3D

    Athomield3D

    Joined:
    Aug 29, 2012
    Posts:
    193
    The different API idea may be the solution here. But I'll definitely look into the other suggestions as well.