Scripting Basic Knowledge |
This page contains some basic advices on scripting. You also can check out Samples and Tutorials.
Read Event Handlers page with simple scripting examples.
In order to write messages to the log and to display log messages in the console of the Player, following methods are used:
Log.Info(string message); Log.Warning(string message);
var scene = Scene.First;
or better way to get by another component of your scene
var scene = component.FindParent<Scene>();
var scene = Scene.First; var obj = scene.GetComponent("MyObject");
var scene = Scene.First; var result = scene.GetComponent("C# Script 2").MethodInvoke("Method", 2, 3); Log.Info(result);
var result = obj.MethodInvoke("MyMethod", 2, 3); Log.Info(result); obj.PropertySet("MyProperty", 5); var value = obj.PropertyGet("MyProperty");
var obj = scene.CreateComponent<Component>(enabled: false); ... // Before enabling, configure the object parameters. obj.Enabled = true;
To delete an object, it must be detached from the object hierarchy. After this action, the object will be completely turned off.
The first way to delete is using RemoveFromParent method which supports a queue of deleted objects. Using a queue allows you to avoid errors in cases when the list of objects is changed while working with it.
obj.RemoveFromParent( true );
You can also delete an object using the Dispose method, although in this case the object cannot be used anymore.
obj.Dispose();
You can add a property directly in the editor.
// Add. var property = scene.CreateComponent<VirtualProperty>(enabled: false); property.Name = "MyProperty"; property.Type = MetadataManager.GetTypeOfNetType(typeof(double)); property.Enabled = true; // Set, get. scene.PropertySet("MyProperty", 5); var value = obj.PropertyGet("MyProperty"); Log.Warning(value);
// Create mesh in space object without enabling. var meshInSpace = scene.CreateComponent<MeshInSpace>(enabled: false); // Set transform. meshInSpace.Transform = new Transform(new Vector3(2, 1, 2), new Angles(0, 0, 45)); // Or set transform by the auxiliary methods (SetPosition, SetRotation, SetScale, LookAt). meshInSpace.SetPosition(2, 1, 2); meshInSpace.SetRotation(0, 0, 45); // Set 3D model. var resourceName = @"Samples\Starter Content\Models\Sci-fi Drone\Sci-fi Drone.fbx"; var meshNameInsideResource = "$Mesh"; meshInSpace.Mesh = ReferenceUtility.MakeReference(resourceName + "|" + meshNameInsideResource); // Enable the object in the scene. meshInSpace.Enabled = true;
// Get object type. var resourceName = @"Samples\Starter Content\Scene objects\Sci-fi Box\Box type.scene"; var objectNameInsideResource = "$Box"; var boxType = MetadataManager.GetType(resourceName + "|" + objectNameInsideResource); // Create the object without enabling. var box = (ObjectInSpace)scene.CreateComponent(boxType, enabled: false); // Set initial position. // If object contains physical body, then need change transform of the body instead of updating the object itself. box.GetComponent<RigidBody>().Transform = new Transform(new Vector3(2, 4, 3), new Angles(0, 0, -45)); // Or set transform by the auxiliary methods (SetPosition, SetRotation, SetScale, LookAt). box.SetPosition(2, 4, 3); box.SetRotation(0, 0, -45); // Enable the object in the scene. box.Enabled = true;