Unity 3D and MDriven

Unity3D use Mono – and Mono builds do not really follow the usual .net versioning with 2.0,3.5 etc… There is a special .net Framework edition you must use. You get this by installing the Visual Studio 2015 Tools for Unity. I would paste link – but I think its better if you google “Visual Studio 2015 Tools for Unity”.

This framework will show up like this:

<a href="//www.capableobjects.com/wp-content/uploads/2016/07/image.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="image" src="//www.capableobjects.com/wp-content/uploads/2016/07/image_thumb.png" alt="image" width="701" height="538" border="0" /></a>

I have added this to our build script and it will be part of the EcoCore package on nuget – so once you have a class library with Target Framework Unity 3.5 .net full Base Class Libraries you can go like this in the package manager console:

<span style="background: white; color: black;">PM> <span style="background-color: #ffff00;">Install-Package EcoCore</span>
Attempting to gather dependency information for package 'EcoCore.7.0.0.8536' with respect to project 'ClassLibrary1', targeting <span style="background-color: #ffff00;">'.NETFramework,Version=v3.5,Profile=Unity Full v3.5'</span>
Attempting to resolve dependencies for package 'EcoCore.7.0.0.8536' with DependencyBehavior 'Lowest'
Resolving actions to install package 'EcoCore.7.0.0.8536'
Resolved actions to install package 'EcoCore.7.0.0.8536'
  GET https://api.nuget.org/packages/ecocore.7.0.0.8536.nupkg
Installing EcoCore 7.0.0.8536.
Adding package 'EcoCore.7.0.0.8536' to folder 'c:\users\hasse\documents\visual studio 2015\Projects\ClassLibrary2\packages'
Added package 'EcoCore.7.0.0.8536' to folder 'c:\users\hasse\documents\visual studio 2015\Projects\ClassLibrary2\packages'
Added package 'EcoCore.7.0.0.8536' to 'packages.config'
Successfully installed 'EcoCore 7.0.0.8536' to ClassLibrary1
</span>

After that you will find the MDriven Assemblies in you project:

<a href="//www.capableobjects.com/wp-content/uploads/2016/07/image1.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="image" src="//www.capableobjects.com/wp-content/uploads/2016/07/image_thumb1.png" alt="image" width="356" height="386" border="0" /></a>

That was requirement 1.

Next step is to provide an EcoSpace and a Model for the project.

I suggest that you add a temporary project that has these – drag them over – then throw away the left overs:

<a href="//www.capableobjects.com/wp-content/uploads/2016/07/image2.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="image" src="//www.capableobjects.com/wp-content/uploads/2016/07/image_thumb2.png" alt="image" width="835" height="566" border="0" /></a>

Also delete the Class1 if you have it – since we don want it and our default model contains another Class1.

<a href="//www.capableobjects.com/wp-content/uploads/2016/07/image3.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="image" src="//www.capableobjects.com/wp-content/uploads/2016/07/image_thumb3.png" alt="image" width="423" height="412" border="0" /></a>

Of course we might want use an existing model – then you would use a file link to this model and Package folder (EcoProject1).

Do a little modeling and then generate code:

<a href="//www.capableobjects.com/wp-content/uploads/2016/07/image4.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="image" src="//www.capableobjects.com/wp-content/uploads/2016/07/image_thumb4.png" alt="image" width="703" height="455" border="0" /></a>

We will still have some errors but they will easily fixed:

<a href="//www.capableobjects.com/wp-content/uploads/2016/07/image5.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="image" src="//www.capableobjects.com/wp-content/uploads/2016/07/image_thumb5.png" alt="image" width="908" height="157" border="0" /></a>

We do not have access to the PersistenceMapperSharer here (its main use is server side when many clients share the same PersistenceMapper). I will change to a PersistenceMapperXml for local storage in an xml file.

<span style="text-decoration: line-through;">    <span style="background: white; color: blue;">private </span></span><span style="background: white; color: black;"><span style="text-decoration: line-through;">Eco.Persistence.PersistenceMapperSharer persistenceMapperSharer1;</span>
    </span><span style="background: white; color: gray;">/// <summary>
    /// </span><span style="background: white; color: green;">Clean up any resources being used.
    </span><span style="background: white; color: gray;">/// </summary>
    </span><span style="background: white; color: blue;">protected override void </span><span style="background: white; color: black;">Dispose(</span><span style="background: white; color: blue;">bool </span><span style="background: white; color: black;">disposing)
    {
      </span><span style="background: white; color: blue;">if </span><span style="background: white; color: black;">(disposing)
      {
        Active = </span><span style="background: white; color: blue;">false</span><span style="background: white; color: black;">;
        </span><span style="background: white; color: blue;">if </span><span style="background: white; color: black;">(</span><span style="background: white; color: blue;">this</span><span style="background: white; color: black;">.components != </span><span style="background: white; color: blue;">null</span><span style="background: white; color: black;">)
        {
          </span><span style="background: white; color: blue;">this</span><span style="background: white; color: black;">.components.Dispose();
        }
      }

      </span><span style="background: white; color: blue;">base</span><span style="background: white; color: black;">.Dispose(disposing);
    }

    </span><span style="background: white; color: blue;">private void </span><span style="background: white; color: black;">InitializeComponent()
    {

      </span><span style="text-decoration: line-through;"><span style="background: white; color: blue;">this</span><span style="background: white; color: black;">.persistenceMapperSharer1 = </span><span style="background: white; color: blue;">new </span></span><span style="text-decoration: line-through;"><span style="background: white; color: black;">Eco.Persistence.PersistenceMapperSharer();
      </span><span style="background: white; color: blue;">this</span><span style="background: white; color: black;">.persistenceMapperSharer1.MapperProviderTypeName = </span><span style="background: white; color: #a31515;">"EcoProject1.EcoSpaceAndModel1.EcoProject1PMP"</span></span><span style="text-decoration: line-through;"><span style="background: white; color: black;">;
      </span><span style="background: white; color: blue;">this</span><span style="background: white; color: black;">.PersistenceMapper = </span><span style="background: white; color: blue;">this</span></span><span style="text-decoration: line-through;"><span style="background: white; color: black;">.persistenceMapperSharer1;
</span></span><span style="background: white; color: black;">
<strong>      Eco.Persistence.</strong></span><strong><span style="background: white; color: #2b91af;">PersistenceMapperXml </span><span style="background: white; color: black;">xmlpm = </span><span style="background: white; color: blue;">new </span><span style="background: white; color: black;">Eco.Persistence.</span><span style="background: white; color: #2b91af;">PersistenceMapperXml</span></strong><strong><span style="background: white; color: black;">();
      xmlpm.FileName = </span><span style="background: white; color: maroon;">@"c:\temp\Gamedata10.xml"</span></strong><strong><span style="background: white; color: black;">;
      </span><span style="background: white; color: blue;">this</span></strong><span style="background: white; color: black;"><strong>.PersistenceMapper = xmlpm;</strong>

    }</span>

That was requirement 2.

To get access to Unity3D I must reference the UnityEngine: I found it here C:\Program Files\Unity\Editor\Data\PlaybackEngines\WindowsStandaloneSupport\Managed\UnityEngine.dll

Once we have that we can construct our Information access

<span style="background: white; color: blue;">using </span><span style="background: white; color: black;">System;
</span><span style="background: white; color: blue;">using </span><span style="background: white; color: black;">System.Collections.Generic;
</span><span style="background: white; color: blue;">using </span><span style="background: white; color: black;">System.Linq;
</span><span style="background: white; color: blue;">using </span><span style="background: white; color: black;">System.Text;
</span><span style="background: white; color: blue;">using </span><span style="background: white; color: black;">UnityEngine;

</span><span style="background: white; color: blue;">namespace </span><span style="background: white; color: black;">ClassLibrary1
{
  </span><span style="background: white; color: blue;">public class </span><span style="background: white; color: #2b91af;">MyScriptThatUseMDriven</span><span style="background: white; color: black;">: </span><span style="background: white; color: #2b91af;">MonoBehaviour
  </span><span style="background: white; color: black;">{
  }
}</span>

MonoBehaviour will be found by the Unity runtime – and it will see if the object has a Start and an Update Method:

    <span style="background: white; color: blue;">void </span><span style="background: white; color: black;">Start()
    {
      </span><span style="background: white; color: #2b91af;">Debug</span><span style="background: white; color: black;">.Log(</span><span style="background: white; color: #a31515;">"Start!!! works?"</span><span style="background: white; color: black;">);
    }

    </span><span style="background: white; color: blue;">void </span><span style="background: white; color: black;">Update()
    {
      </span><span style="background: white; color: #2b91af;">Debug</span><span style="background: white; color: black;">.Log(</span><span style="background: white; color: #a31515;">"works?"</span><span style="background: white; color: black;">);
    }
</span>

Now let us start Unity – create a project or find the project we want to use our information in.

<a href="//www.capableobjects.com/wp-content/uploads/2016/07/image6.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="image" src="//www.capableobjects.com/wp-content/uploads/2016/07/image_thumb6.png" alt="image" width="644" height="373" border="0" /></a>

I need to make my Assmbly and its dependcies available to unity.

I set a build event on my project to copy over everything from my bin folder – but I set the “Copy Local” of UnityEngine.dll to FALSE. This is because Unity will have its own copy of this important assembly:

<a href="//www.capableobjects.com/wp-content/uploads/2016/07/image7.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="image" src="//www.capableobjects.com/wp-content/uploads/2016/07/image_thumb7.png" alt="image" width="644" height="424" border="0" /></a>

The text in Post-Build is: “xcopy "$(TargetDir)*.*" "C:\temp\TheMDrivenTestProject1000\Assets\MyVSAssemblies" /Y”  (I created a Folder in Unity called MyVSAssemblies)

I now build my assembly in VS and then the post-build-event copies the result to my Unity project. As a result Unity should find out class that implements MonoBehaviour:

<a href="//www.capableobjects.com/wp-content/uploads/2016/07/image8.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="image" src="//www.capableobjects.com/wp-content/uploads/2016/07/image_thumb8.png" alt="image" width="644" height="366" border="0" /></a>

To get Unity to find any reason for actually executing our script as part of the game – we must associate the script with a GameObject. So I add an empty GameObject that has the only purpose to be a reference to our script.

 

<a href="//www.capableobjects.com/wp-content/uploads/2016/07/image9.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="image" src="//www.capableobjects.com/wp-content/uploads/2016/07/image_thumb9.png" alt="image" width="644" height="232" border="0" /></a>

I then drag my script onto this game object:

<a href="//www.capableobjects.com/wp-content/uploads/2016/07/image10.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="image" src="//www.capableobjects.com/wp-content/uploads/2016/07/image_thumb10.png" alt="image" width="644" height="158" border="0" /></a>

I can now verify that the script works by hitting Play in Unity and watch for my Debug.Log messages:

<a href="//www.capableobjects.com/wp-content/uploads/2016/07/image11.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="image" src="//www.capableobjects.com/wp-content/uploads/2016/07/image_thumb11.png" alt="image" width="630" height="436" border="0" /></a>

So we have come pretty far. Now we can now make a prefab in Unity (look up this on a Unity blog) and call it “Player”.

Oh and one note about the Console – you will find compilation errors here like this:

<a href="//www.capableobjects.com/wp-content/uploads/2016/07/image12.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="image" src="//www.capableobjects.com/wp-content/uploads/2016/07/image_thumb12.png" alt="image" width="644" height="344" border="0" /></a>

In this case it says I am missing a reference to WindowsBase – and they I need to add it – and set Copy Local.

I create a Prefab and call it MyCarPrefab:

<a href="//www.capableobjects.com/wp-content/uploads/2016/07/image13.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="image" src="//www.capableobjects.com/wp-content/uploads/2016/07/image_thumb13.png" alt="image" width="220" height="244" border="0" /></a>

It has a Text Mesh and I use that to show the score. In Start I create prefabs from my Player objects, and spread them over the terrain:

    <span style="background: white; color: blue;">void </span><span style="background: white; color: black;">Start()
    {
      </span><span style="background: white; color: #2b91af;">Debug</span><span style="background: white; color: black;">.Log(</span><span style="background: white; color: #a31515;">"Start!!! works?"</span><span style="background: white; color: black;">);

      _es=</span><span style="background: white; color: blue;">new </span><span style="background: white; color: #2b91af;">EcoProject1EcoSpace</span><span style="background: white; color: black;">();
      _es.Active = </span><span style="background: white; color: blue;">true</span><span style="background: white; color: black;">;

      </span><span style="background: white; color: blue;">var </span><span style="background: white; color: black;">players=_es.Extents.AllInstances<</span><span style="background: white; color: #2b91af;">Player</span><span style="background: white; color: black;">>();

      </span><span style="background: white; color: blue;">int </span><span style="background: white; color: black;">c = 0;
      </span><span style="background: white; color: blue;">foreach </span><span style="background: white; color: black;">(</span><span style="background: white; color: blue;">var </span><span style="background: white; color: black;">x </span><span style="background: white; color: blue;">in </span><span style="background: white; color: black;">players)
      {
        c++;
        </span><span style="background: white; color: #2b91af;">GameObject </span><span style="background: white; color: black;">go = (</span><span style="background: white; color: #2b91af;">GameObject</span><span style="background: white; color: black;">)Instantiate(</span><span style="background: white; color: #2b91af;">Resources</span><span style="background: white; color: black;">.Load(</span><span style="background: white; color: #a31515;">"MyCarPrefab"</span><span style="background: white; color: black;">));
        </span><span style="background: white; color: blue;">var </span><span style="background: white; color: black;">p = go.transform.position;
        p.Set(p.x + (c * 2.5f), p.y + (c * 2), p.z</span><span style="background: white; color: green;">/*+ (c * 1)*/</span><span style="background: white; color: black;">);
        go.transform.position = p;
        go.GetComponentInChildren<</span><span style="background: white; color: #2b91af;">TextMesh</span><span style="background: white; color: black;">>().text = x.Score.ToString();
      }

    }
</span>

Put since I do not have any Player-objects yet I turn to the MDriven debugger to create a few in the xml file c:\temp\Gamedata10.xml

<a href="//www.capableobjects.com/wp-content/uploads/2016/07/image14.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="image" src="//www.capableobjects.com/wp-content/uploads/2016/07/image_thumb14.png" alt="image" width="644" height="351" border="0" /></a>

Lastly I save them by switching to DirtyObjects tab and press Update selected:

<a href="//www.capableobjects.com/wp-content/uploads/2016/07/image15.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="image" src="//www.capableobjects.com/wp-content/uploads/2016/07/image_thumb15.png" alt="image" width="681" height="449" border="0" /></a>

And then I get my cars and their scores like this:

<a href="//www.capableobjects.com/wp-content/uploads/2016/07/image16.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" title="image" src="//www.capableobjects.com/wp-content/uploads/2016/07/image_thumb16.png" alt="image" width="741" height="570" border="0" /></a>

 

Ok – that’s it for now

We also did this clip that shows the process:

This page was edited 70 days ago on 02/10/2024. What links here