Unity 3D and MDriven
No edit summary
No edit summary
(2 intermediate revisions by one other user not shown)
Line 2: Line 2:


This framework will show up like this:
This framework will show up like this:
[[File:Unity -1 .png|none|frame|355x355px]]
[[File:Unity -1 .png|none|frame|510x510px]]


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:
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:
Line 8: Line 8:
<pre class="code"><span style="background: white; color: black;">PM&gt; <span style="background-color: #ffff00;">Install-Package EcoCore</span>
<pre class="code"><span style="background: white; color: black;">PM&gt; <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 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>
<br>Attempting to resolve dependencies for package 'EcoCore.7.0.0.8536' with DependencyBehavior 'Lowest'
Attempting to resolve dependencies for package 'EcoCore.7.0.0.8536' with DependencyBehavior 'Lowest'
<br>Resolving actions to install package 'EcoCore.7.0.0.8536'
Resolving actions to install package 'EcoCore.7.0.0.8536'
<br>Resolved actions to install package 'EcoCore.7.0.0.8536'
Resolved actions to install package 'EcoCore.7.0.0.8536'
<br>  GET https://api.nuget.org/packages/ecocore.7.0.0.8536.nupkg
  GET https://api.nuget.org/packages/ecocore.7.0.0.8536.nupkg
<br>Installing EcoCore 7.0.0.8536.
Installing EcoCore 7.0.0.8536.
<br>Adding package 'EcoCore.7.0.0.8536' to folder 'c:\users\hasse\documents\visual studio 2015\Projects\ClassLibrary2\packages'
Adding package 'EcoCore.7.0.0.8536' to folder 'c:\users\hasse\documents\visual studio 2015\Projects\ClassLibrary2\packages'
<br>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 folder 'c:\users\hasse\documents\visual studio 2015\Projects\ClassLibrary2\packages'
<br>Added package 'EcoCore.7.0.0.8536' to 'packages.config'
Added package 'EcoCore.7.0.0.8536' to 'packages.config'
<br>Successfully installed 'EcoCore 7.0.0.8536' to ClassLibrary1
Successfully installed 'EcoCore 7.0.0.8536' to ClassLibrary1
</span></pre> </html>
</span></pre> </html>


After that you will find the MDriven Assemblies in you project:
After that you will find the MDriven Assemblies in you project:
 
[[File:Unity - 2.png|none|frame|297x297px]]
 


That was requirement 1.
That was requirement 1.
Line 28: Line 27:


I suggest that you add a temporary project that has these – drag them over – then throw away the left overs:
I suggest that you add a temporary project that has these – drag them over – then throw away the left overs:
[[File:Unity - 3.png|none|frame|464x464px]]




Also delete the Class1 if you have it – since we don't want it and our default model contains another Class1.
Also delete the Class1 if you have it – since we don't want it and our default model contains another Class1.
[[File:Unity - 4.png|none|frame|283x283px]]




Line 36: Line 37:


Do a little modeling and then generate code:
Do a little modeling and then generate code:
 
[[File:Unity - 5.png|none|frame|409x409px]]




We will still have some errors but they will easily fixed:
We will still have some errors but they will easily fixed:
 
[[File:Unity - 6.png|none|frame|586x586px]]
 


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.
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.
Line 110: Line 110:


Now let us start Unity – create a project or find the project we want to use our information in.
Now let us start Unity – create a project or find the project we want to use our information in.
 
[[File:Unity - 7.png|none|frame|513x513px]]
 


I need to make my Assmbly and its dependcies available to unity.
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:
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:
 
[[File:Unity - 8.png|none|frame|431x431px]]


The text in Post-Build is: “xcopy "$(TargetDir)*.*" "C:\temp\TheMDrivenTestProject1000\Assets\MyVSAssemblies" /Y”  (I created a Folder in Unity called MyVSAssemblies)
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:
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:
 
[[File:Unity - 9.png|none|frame|435x435px]]
 


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.
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.
 
[[File:Unity - 10.png|none|frame|434x434px]]
&nbsp;
 
 


I then drag my script onto this game object:
I then drag my script onto this game object:
 
[[File:Unity - 11.png|none|frame|441x441px]]
 


I can now verify that the script works by hitting Play in Unity and watch for my Debug.Log messages:
I can now verify that the script works by hitting Play in Unity and watch for my Debug.Log messages:
 
[[File:Unity - 12.png|none|frame|455x455px]]
 


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”.
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:
Oh and one note about the Console – you will find compilation errors here like this:
 
[[File:Unity - 13.png|none|frame|472x472px]]


In this case it says I am missing a reference to WindowsBase – and they I need to add it – and set Copy Local.
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:
I create a Prefab and call it MyCarPrefab:
 
[[File:Unity - 14.png|none|frame]]




Line 175: Line 168:
</html>
</html>
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
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
 
[[File:Unity - 15.png|none|frame|387x387px]]
 
 
Lastly I save them by switching to DirtyObjects tab and press Update selected:
Lastly I save them by switching to DirtyObjects tab and press Update selected:
 
[[File:Unity - 16.png|none|frame|390x390px]]
 


And then I get my cars and their scores like this:
And then I get my cars and their scores like this:
 
[[File:Unity - 17.png|none|frame|286x286px]]
 
 
&nbsp;


Ok – that’s it for now
Ok – that’s it for now
Line 196: Line 183:
<div class="video">
<div class="video">
   <div class="video__wrapper">
   <div class="video__wrapper">
<iframe src="https://www.youtube.com/embed/cAqq0LIvIHM?rel=0&autoplay=0" frameborder="0" allowfullscreen></iframe>
<iframe src="https://www.youtube.com/embed/ytkSQ-ppzfo?rel=0&autoplay=0" frameborder="0" allowfullscreen></iframe>
</div>
</div>
<div class="video__navigation">
<div class="video__navigation">

Revision as of 20:27, 9 November 2018

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:

Unity -1 .png

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:

PM> Install-Package EcoCore
Attempting to gather dependency information for package 'EcoCore.7.0.0.8536' with respect to project 'ClassLibrary1', targeting '.NETFramework,Version=v3.5,Profile=Unity Full v3.5'
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

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

Unity - 2.png

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:

Unity - 3.png


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

Unity - 4.png


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:

Unity - 5.png


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

Unity - 6.png

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.

    private Eco.Persistence.PersistenceMapperSharer persistenceMapperSharer1;
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose(bool disposing)
    {
      if (disposing)
      {
        Active = false;
        if (this.components != null)
        {
          this.components.Dispose();
        }
      }

      base.Dispose(disposing);
    }

    private void InitializeComponent()
    {

      this.persistenceMapperSharer1 = new Eco.Persistence.PersistenceMapperSharer();
      this.persistenceMapperSharer1.MapperProviderTypeName = "EcoProject1.EcoSpaceAndModel1.EcoProject1PMP";
      this.PersistenceMapper = this.persistenceMapperSharer1;

      Eco.Persistence.PersistenceMapperXml xmlpm = new Eco.Persistence.PersistenceMapperXml();
      xmlpm.FileName = @"c:\temp\Gamedata10.xml";
      this.PersistenceMapper = xmlpm;

    }

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace ClassLibrary1
{
  public class MyScriptThatUseMDriven: MonoBehaviour
  {
  }
}

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

    void Start()
    {
      Debug.Log("Start!!! works?");
    }

    void Update()
    {
      Debug.Log("works?");
    }

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

Unity - 7.png

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:

Unity - 8.png

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:

Unity - 9.png

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.

Unity - 10.png

I then drag my script onto this game object:

Unity - 11.png

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

Unity - 12.png

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:

Unity - 13.png

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:

Unity - 14.png


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:

    void Start()
    {
      Debug.Log("Start!!! works?");

      _es=new EcoProject1EcoSpace();
      _es.Active = true;

      var players=_es.Extents.AllInstances<Player>();

      int c = 0;
      foreach (var x in players)
      {
        c++;
        GameObject go = (GameObject)Instantiate(Resources.Load("MyCarPrefab"));
        var p = go.transform.position;
        p.Set(p.x + (c * 2.5f), p.y + (c * 2), p.z/*+ (c * 1)*/);
        go.transform.position = p;
        go.GetComponentInChildren<TextMesh>().text = x.Score.ToString();
      }

    }

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

Unity - 15.png

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

Unity - 16.png

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

Unity - 17.png

Ok – that’s it for now

We also did this clip that shows the process:

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