Document toolboxDocument toolbox

2021.1.1560.3 NPrecisionMonoBehavior



1. Overview

The N-Precision Monobehavior base-class allows you to quickly create a new Unity3D Monobehavior that manages transformations for Double- or Single Precision automatically, and allows the end user to switch between double- and single precision mode in both editor and runtime.

As an end-user you rarely care about how your transformations are applied onto the Game-object, however you do want them to be applied properly, and without breaking the different transformation systems that exist inside unity - the NPrecisionMonobehavior serves this particular interface function.


2. Inspector Fields Exposed by the NPrecisionMonobehavior

If you implement the NPrecisionMonobehavior the following fields will be added to your Inspector

Note, these fields are defined in a custom UIElements Editor script, so if you create your own editor script it may look different

Overview of the N Precision Monobehavior & Transform Inspector in ‘Floating Point’ Precision Mode

Overview of the N Precision Monobehavior & DTransform Inspector in ‘double Point’ Precision Mode

Inspector Field

Description

Inspector Field

Description

Use Rigidbody

Toggles whether you want the transformations on this NPrecision Monobehavior to run via the Rigidbody (Unity Physics)

Rigidbody

The Rigidbody component instance currently used for assigning transformations when ‘Use Rigidbody’ is active

Transform Precision

The current transform precision you want to use for this gameobject - default is floating point (32-bits) which will force the use of the regular ‘Transform’ component, if you set it to ‘Double’ (64-bits) the DTransform component will be automatically assigned


3. Creating a script that inherits NPrecisionMonobehavior

The following sample code shows how to create your own inheritant NPrecision Monobehavior Class:

using System; using u040.prespective.core; namespace u040.prespective.nprecisiondemo { public class NPrecisionMonobehaviorDemo : NPrecisionMonoBehavior { /// <summary> /// The Child types that should automatically also toggle if we change the precision /// setting on this monobehavior in the inspector /// </summary> public override Type[] NPrecisionAffectedChildComponentTypes => null; /// <summary> /// This function is called when Unity enters playmode, and should make sure that - /// if we want this gameobjects' behavior to be exhibited via the Rigidbody (Unity Physics) /// that it is setup properly (e.g. make sure its setup kinematic) /// </summary> public override void SetupAssignedRigidBody() { //Don't change anything on the Rigidbody setup at startup } } }

For the override class to work it expects you to override 2 specific members:

Member

Member Intent

Example

Member

Member Intent

Example

NPrecisionAffectedChildComponentTypes

Often your script makes up one part of a feature; in such an event, changing the fidelity setting (double or float) should also be applied accross specific NPrecisionMonoBehavior types that exist in child gameobjects.

This override allows you to target specific types in child objects that will be automatically changed to the same fidelity, if you change it on this object.

The DSpline class is an inheritant of NPrecisionMonoBehavior, obviously if we change the precision to double on the DSpline we want this to also be applied on the contained controlpoints.

As such the member looks as following:

public override Type[] NPrecisionAffectedChildComponentTypes => new Type[]{ typeof(ADSplineControlPoint)};

SetupAssignedRigidBody

If we setup our NPrecisionMonobehavior to target Rigidbody transformation (to work with the Unity Physics System) we may need to enforce specific settings on the used Rigidbody. This override is called on entering playmode

DES Participants (Actors, Cues) will only work properly if operate as kinematic rigidbodies. therfore the override of this function in DESParticipant is as follows:

public override void SetupAssignedRigidBody() { RigidBody.isKinematic = true; }

In Unity2020 an error may occur when you try to assign an instance of this script to a gameobject': ‘The script don't inherit a native class that can manage a script’. This is in fact a bug in Unity 2020 - the easiest workaround for this bug is to comment out the namespace in the sample, then try applying it to a gameobject (which should work now).



 

 

Prespective Documentation