「Virt-A-Mate – Hello World – plugin basics 1 をやってみる。 – その1」の続き。
Hello World – plugin basics 1をやってみている。
今回はHello World – plugin basics 1からもらってきたスクリプトを眺めてみる。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; //using System.Threading.Tasks; // VaM throws an error when this is left included. Not needed? namespace NoButYeaNS { class NBYPluginTest01: MVRScript { protected JSONStorableFloat jsonFloat; protected UIDynamicButton myButton; protected Int16 counterValue; // store the value of our counter protected Boolean counterValueIsValid; // set to false upon making a change. So we can recoginse a change was made in Update() protected float sliderValue; // tracks the slider value (frame to frame) public override void Init() { pluginLabelJSON.val = "NoButYea Test 1.0"; // CreateSlider needs a JSONStorableFloat value so.. // Initialize storable values. jsonFloat = new JSONStorableFloat("jsonFloat", 0.0f, 0.0f, 1.0f); RegisterFloat(jsonFloat); // Registering the float is important -- if not they wont save CreateSlider(jsonFloat, true); // the second argument is for 'rightSide'. Supplying true will render the slider on the right-side // track the value of the slider so that... // we can track if a change was made since last frame. sliderValue = jsonFloat.val; // initialize to whatever the slider is now. // Initialize the counter (that the button will influence) counterValue = 0; counterValueIsValid = false; // Add a button and a click handler myButton = CreateButton("My Button", false); myButton.height = 100; myButton.button.onClick.AddListener(delegate () { // increase counter value counterValue++; counterValueIsValid = false; // so we can detect a change was made }); } // Runs once when plugin loads - after Init() protected void Start() { // show a message SuperController.LogMessage(pluginLabelJSON.val + " Loaded"); } // A Unity thing - runs every physics cycle public void FixedUpdate() { // put code here } // Unity thing - runs every rendered frame public void Update() { if(counterValueIsValid == false) // check if counter has changed since last frame { SuperController.LogMessage("counter change detected: " + counterValue); counterValueIsValid = true; // set to true to avoid log message every frame } if(jsonFloat.val != sliderValue) // check if slider was changed since last frame { sliderValue = jsonFloat.val; // update our tracking value SuperController.LogMessage("slider value change detected: " + sliderValue); } } } } |
コメントがあるので何をやっているのかは理解できるが、これを1から自分で書けるかどうかといえばまず無理だと思う。
Overview of the methods in the class…
Init – set the default values for properties we have defined
Start – just print a message to say the plugin is loaded
FixedUpdate – nothing yet
Update – each frame, check for changes and print a message when something changes (rather than continuous messages – every frame)
「各ブロックが何をするためのものかは上のように決まっている。」
Side note: to know what ‘type’ you should use when creating a variable that references a UI component (such as a Slider or Button); Start typing the word ‘Create’ and observe the return type from intellisense.
Object BrowserのSearchに、例えばCreateとか入力すれば、ここで使っているCreateSliderのTypeなどを知ることができる。
コメントにも書いてあるが、例えば2つ目の引数がFalseでSliderは右側に配置される、というようなこともわかる。
あとは
やりたいことをどのstuffで実現するかを調べて、物まねしてコードを書いていくしかないだろう。
例えばもう1つSliderを用意して今度は左側に配置してみるとか。
Once the script is saved, drop the .cs file somewhere in Custom\Scripts\
e.g.
\Custom\Scripts\YouAuthorName\YourClass.cs
Open VaM, load a scene, find the plugins window, (either scene plugin, session plugin or select an atom).
Click to add Plugin and choose your .cs file from the file explorer.
Click ‘Open Custom UI…’ to see the Button and Slider.
You should see changes to the counter variable or slider outputted in a message box.
「Saveしたら適当なところに置いてVaMから呼び出して試してみよう。」
If your plugin is RED and you see an error log – check the imports at the top of your script file against mine.
「?」
Also, be sure to check the .cs files from other author plugins (just unpack the var with 7-zip) for example code and inspiration.
「他人のcsファイルを見てみるのもお薦めです。」
For my first non-test plugin, I’ll be attempting to do something with VR controller positional metrics. If that works out ill reference to it here.
Update: click here for part 2 – November 2021:
TweetHope this helps someone – DM me with any inaccuracies because ill edit the guide.
Any questions or problems with the plugin/script or loading, comment here for other user benefit – they might have the same issues.
Regards