Code Monkey home page Code Monkey logo

angular-unity's Introduction

angular-unity

Example Angular and Unity integration using:

  • Unity 2019.3.14f1
  • Angular 12.0.x

Installation

npm install

Usage

npm start

Then view the site at:

http://localhost:4200/

Creating a compatible Unity project

To communicate between JavaScript and Unity you need a few things:

  1. Create a Unity Project with GameObject > Controller.cs:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System.Runtime.InteropServices;

    public class Controller : MonoBehaviour {

        [DllImport("__Internal")]
        private static extern void SendMessageToWeb(string msg);

        public void ReceiveMessageFromWeb(string msg) {
            Debug.Log("Controller.ReceiveMessageFromWeb: " + msg);
        }

        // Use this for initialization
        void Start() {
            SendMessageToWeb("Hello from Unity");
        }

        // Update is called once per frame
        void Update() {

        }
    }
  1. Add a file to the Unity project at: /Assets/Plugins/WebInterface.jslib containing:
    mergeInto(LibraryManager.library, {
    SendMessageToWeb: function (str) {
        window.receiveMessageFromUnity(str);
    },
    });
  1. Build the project as WebGL so that it creates the files:
  • demo.data.unityweb
  • demo.json
  • demo.wasm.code.unityweb
  • demo.wasm.framework.unityweb
  1. Copy the generated files to this project folder:

    /src/assets

  2. Embed your generated files using the reusable Angular component:

Updating Unity

If you update Unity then you'll also needs to update the Unity JavaScript files to match. Publish a WebGL project and copy the new JavaScript files into the angular src folder here:

./src/assets/UnityLoader.js
./src/assets/UnityProgress.js

Directory structure

src/                       --> Frontend sources files
unity-src/                 --> Unity script examples

Contact

For more information please contact kmturley

angular-unity's People

Contributors

kmturley 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.