Git Product home page Git Product logo

runtimescriptfield_unity's Introduction

RuntimeScriptField_Unity

A utility for Unity allowing you to assign scripts in the editor, and add them to game objects at runtime.

Usage

Copy Assets/Plugins into your own Assets/Plugins

Declare a field of the type ComponentReference, and use it at runtime:

using RuntimeScriptField;

public class Example : MonoBehaviour
{
    public ComponentReference scriptRef;
    
    void Start() {
        scriptRef.AddTo(gameObject);
    }
}

You can drag-and-drop any script into the scriptRef field in the inspector, as long as the script contains a Component.

If you want more or less control over what's assignable to the script, you can inherit from ScriptReference:

using RuntimeScriptField;

[System.Serializable]
public class FooReference : ScriptReference<Foo> { }

public class Example2 : MonoBehaviour
{
    public FooReference fooRef;
    
    void Start() {
        Debug.Log("The script you assigned to fooRef is: " + fooRef.script);
    }
}

In this case, only scripts containing something that inherits from Foo can be assigned to the field. ComponentReference is simply a ScriptReference with some utility methods.

Why

There has been a lot of forum threads throughout the years where people ask for exactly something like this. I figured I might as well share a solution.

Contributions

Please! This is a very simple project, but if you feel something's missing, please send a pull request!

Bugs

Please report any bug you find!

Thanks to

Bryan Keiren for Serializable System Type: http://bryankeiren.com/files_public/UnityScripts/SerializableSystemType.cs Dylan Engelman (lordofduct) for SerializedProperty tools: https://github.com/lordofduct/spacepuppy-unity-framework/blob/master/SpacepuppyBaseEditor/EditorHelper.cs

runtimescriptfield_unity's People

Contributors

baste-raingames avatar bbuanes avatar

Watchers

 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.