Git Product home page Git Product logo

classtypereference-for-unity's Introduction

README

A class which provides serializable references to System.Type of classes with an accompanying custom property drawer which allows class selection from drop-down.

screenshot

Whilst we have not encountered any platform specific issues yet, the source code in this repository might not necessarily work for all of Unity's platforms or build configurations. It would be greatly appreciated if people would report issues using the issue tracker.

This is a fork of the currently inactive project by Rotorz: ClassTypeReference for Unity

Install via Git URL

Project supports Unity Package Manager. To install the project as a Git package do the following:

  1. In Unity, open Window -> Package Manager.
  2. Press the + button, choose "Add package from git URL..."
  3. Enter "https://github.com/SolidAlloy/ClassTypeReference-for-Unity.git#upm" and press Add.

Usage Examples

Type references can be made using the inspector simply by using ClassTypeReference:

using UnityEngine;
using TypeReferences;

public class ExampleBehaviour : MonoBehaviour
{
    public ClassTypeReference greetingLoggerType;
}

A default value can be specified in the normal way:

public ClassTypeReference greetingLoggerType = typeof(DefaultGreetingLogger);

You can apply one of two attributes to drastically reduce the number of types presented when using the drop-down field.

using UnityEngine;
using TypeReferences;

public class ExampleBehaviour : MonoBehaviour
{
    // Allow selection of classes that implement an interface.
    [ClassImplements(typeof(IGreetingLogger))]
    public ClassTypeReference greetingLoggerType;

    // Allow selection of classes that extend a specific class.
    [ClassExtends(typeof(MonoBehaviour))]
    public ClassTypeReference someBehaviourType;
}

To create an instance at runtime you can use the System.Activator class from the .NET / Mono library:

using System;
using UnityEngine;
using TypeReferences;

public class ExampleBehaviour : MonoBehaviour
{
    [ClassImplements(typeof(IGreetingLogger))]
    public ClassTypeReference greetingLoggerType = typeof(DefaultGreetingLogger);

    private void Start()
    {
        if (greetingLoggerType.Type == null)
        {
            Debug.LogWarning("No type of greeting logger was specified.");
        }
        else
        {
            var greetingLogger = Activator.CreateInstance(greetingLoggerType) as IGreetingLogger;
            greetingLogger.LogGreeting();
        }
    }
}

Presentation of drop-down list can be customized by supplying a ClassGrouping value to any of the attributes: ClassTypeConstraint, ClassImplements or ClassExtends.

  • ClassGrouping.None - No grouping, just show type names in a list; for instance, "Some.Nested.Namespace.SpecialClass".

  • ClassGrouping.ByNamespace - Group classes by namespace and show foldout menus for nested namespaces; for instance, "Some > Nested > Namespace > SpecialClass".

  • ClassGrouping.ByNamespaceFlat (default) - Group classes by namespace; for instance, "Some.Nested.Namespace > SpecialClass".

  • ClassGrouping.ByAddComponentMenu - Group classes in the same way as Unity does for its component menu. This grouping method must only be used for MonoBehaviour types.

For instance,

using UnityEngine;
using TypeReferences;

public class ExampleBehaviour : MonoBehaviour
{
    [ClassImplements(typeof(IGreetingLogger), Grouping = ClassGrouping.ByAddComponentMenu)]
    public ClassTypeReference greetingLoggerType;
}

You can exclude (None) so that no one can choose it from the dropdown. Use it with any of the attributes like this:

using UnityEngine;
using TypeReferences;

public class ExampleBehaviour : MonoBehaviour
{
    [ClassTypeConstraint(ExcludeNone = true)]
    public ClassTypeReference someTypeExample;
}

Note that the type can still be null by default or if set through code.

Credits

Contribution Agreement

This project is licensed under the MIT license (see LICENSE). To be in the best position to enforce these licenses the copyright status of this project needs to be as simple as possible. To achieve this the following terms and conditions must be met:

  • All contributed content (including but not limited to source code, text, image, videos, bug reports, suggestions, ideas, etc.) must be the contributors own work.

  • The contributor disclaims all copyright and accepts that their contributed content will be released to the public domain.

  • The act of submitting a contribution indicates that the contributor agrees with this agreement. This includes (but is not limited to) pull requests, issues, tickets, e-mails, newsgroups, blogs, forums, etc.

classtypereference-for-unity's People

Contributors

kruncher avatar max99x avatar solidalloy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.