docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Randomizers

    Randomizers encapsulate specific randomization activities to perform during the execution of a randomized simulation. For example, Randomizers exist for spawning objects, repositioning lights, varying the color of objects, etc. Randomizers expose random parameters to their inspector interface to further customize these variations. Users can add a set of Randomizers to a Scenario in order to define an ordered list of randomization activities to perform during the lifecycle of a simulation.

    To define an entirely new Randomizer, derive the Randomizer class and implement one or more of the methods listed in the section below to randomize GameObjects during the runtime of a simulation.

    Randomizer Hooks

    1. OnCreate() - called when the Randomizer is added or loaded to a Scenario
    2. OnIterationStart() - called at the start of a new Scenario Iteration
    3. OnIterationEnd() - called the after a Scenario Iteration has completed
    4. OnScenarioComplete() - called the after the entire Scenario has completed
    5. OnStartRunning() - called on the first frame a Randomizer is enabled
    6. OnStopRunning() - called on the first frame a disabled Randomizer is updated
    7. OnUpdate() - executed every frame for enabled Randomizers

    Randomizer Coding Example

    Below is the code for the sample rotation Randomizer included with the Perception package:

    [Serializable]
    [AddRandomizerMenu("Perception/Rotation Randomizer")]
    public class RotationRandomizer : Randomizer
    {
        public Vector3Parameter rotation = new Vector3Parameter();
    
        protected override void OnIterationStart()
        {
            var taggedObjects = tagManager.Query<RotationRandomizerTag>();
            foreach (var taggedObject in taggedObjects)
                taggedObject.transform.rotation = Quaternion.Euler(rotation.Sample());
        }
    }
    

    There are a few key things to note from this example:

    1. Make sure to add the [Serializable] tag to all Randomizer implementations to ensure that the Randomizer can be customized and saved within the Unity Editor.
    2. The [AddRandomizerMenu] attribute customizes the "Add Randomizer" sub menu path in the Scenario inspector for a particular Randomizer. In this example, the RotationRandomizer can be added to a Scenario by opening the Add Randomizer menu and clicking Perception -> Rotation Randomizer.
    3. The line var taggedObjects = tagManager.Query<RotationRandomizerTag>(); uses RandomizerTags in combination with the current Scenario's tagManager to query for all objects with RotationRandomizerTags and obtain the subset of GameObjects within the simulation that need to have their rotations randomzied. To learn more about how RandomizerTags work, visit the RandomizerTags documentation page.

    Did you find this page useful? Please give it a rating:

    Thanks for rating this page!

    Report a problem on this page

    What kind of problem would you like to report?

    • This page needs code samples
    • Code samples do not work
    • Information is missing
    • Information is incorrect
    • Information is unclear or confusing
    • There is a spelling/grammar error on this page
    • Something else

    Thanks for letting us know! This page has been marked for review based on your feedback.

    If you have time, you can provide more information to help us fix the problem faster.

    Provide more information

    You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:

    You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:

    You've told us there is information missing from this page. Please tell us more about what's missing:

    You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:

    You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:

    You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:

    You've told us this page has a problem. Please tell us more about what's wrong:

    Thank you for helping to make the Unity documentation better!

    Your feedback has been submitted as a ticket for our documentation team to review.

    We are not able to reply to every ticket submitted.

    In This Article
    • Randomizer Hooks
    • Randomizer Coding Example
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)