1
0
mirror of https://github.com/Memorix101/UnityXNA/ synced 2024-12-30 15:25:35 +01:00
Barnaby Smith 6fe889760d First commit. Proof of concept implementation.
The XNA 4.0 PlatformerGame sample is successfully running inside Unity3D
3.5.
Implemented a basic game loop, game timing, content loading for
Texture2D, SoundEffect and Song. Emulated SpriteBatch drawing for
sprites and strings (note SpriteFont is not yet supported to all strings
are rendered using the default GUI label font). Songs can be played
using an AudioSource attached to the XNATest game object which acts as
an emulator for MediaPlayer. Playing a SoundEffect creates a game object
with an AudioSource attached which is automatically deleted when the
sound finishes. Implemented keyboard input with a limited set of XNA
Keys mapping to Unity3D KeyCodes.
2012-07-07 20:57:54 +01:00

149 lines
4.1 KiB
C#

#define ALT_MODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Audio;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
namespace Microsoft.Xna.Framework.Content
{
public class ContentManager
{
private IServiceProvider serviceProvider;
private string p;
public ContentManager(IServiceProvider serviceProvider, string p)
{
// TODO: Complete member initialization
this.serviceProvider = serviceProvider;
this.p = p;
}
internal T1 Load<T1>(string asset) where T1 : IDisposable
{
Type type = typeof(T1);
#if ALT_MODE
asset = Path.Combine("Content", asset);
asset = asset.Replace("\\","/");
#else
asset = Path.Combine(Directory.GetCurrentDirectory(), Path.Combine("Content", asset));
string filename;
string directoryName = Path.GetDirectoryName(asset);
string smallFilename = Path.GetFileName(asset);
string[] fileResults = Directory.GetFiles(directoryName, smallFilename + "*");
asset = fileResults[0];
#endif
// TODO: Do cache check
if(type == typeof(Texture2D))
{
Texture2D texture = LoadTexture2D(asset);
return (T1)Convert.ChangeType(texture, type);
}
else if (type == typeof(SpriteFont))
{
SpriteFont spriteFont = LoadSpriteFont(asset);
return (T1)Convert.ChangeType(spriteFont, type);
}
else if (type == typeof(SoundEffect))
{
SoundEffect soundEffect = LoadSoundEffect(asset);
return (T1)Convert.ChangeType(soundEffect, type);
}
else if (type == typeof(Song))
{
Song song = LoadSong(asset);
return (T1)Convert.ChangeType(song, type);
}
// TODO: Improve
return default(T1);
}
private Texture2D LoadTexture2D(string asset)
{
UnityEngine.Texture2D unityTexture = new UnityEngine.Texture2D(2, 2);
#if ALT_MODE
unityTexture = (UnityEngine.Texture2D)UnityEngine.Resources.Load(asset);
#else
byte[] bytes = File.ReadAllBytes(asset);
unityTexture.LoadImage(bytes);
#endif
return new Texture2D(unityTexture);
}
private SpriteFont LoadSpriteFont(string asset)
{
UnityEngine.TextAsset spriteFontText = (UnityEngine.TextAsset)UnityEngine.Resources.Load(asset, typeof(UnityEngine.TextAsset));
Dictionary<string, string> fontSettings = new Dictionary<string, string>();
StringReader stringReader = new StringReader(spriteFontText.text);
stringReader.Read(); // Skip the byte order mark - seems to cause Unity issues
XmlTextReader xmlReader = new XmlTextReader(stringReader);
XmlDocument document = new XmlDocument();
document.Load(xmlReader);
foreach(XmlNode childNode in document.DocumentElement.ChildNodes[0].ChildNodes)//[0].ChildNodes)
{
// Ignore comments
if(!childNode.Name.StartsWith("#"))
{
fontSettings.Add(childNode.Name, childNode.InnerText);
}
}
xmlReader.Close();
stringReader.Close();
if(fontSettings.ContainsKey("FontName")
&& fontSettings.ContainsKey("Size")
&& fontSettings.ContainsKey("Spacing")
&& fontSettings.ContainsKey("UseKerning")
&& fontSettings.ContainsKey("Style"))
{
SpriteFont spriteFont = new SpriteFont(fontSettings["FontName"], float.Parse(fontSettings["Size"]), float.Parse(fontSettings["Spacing"]), bool.Parse(fontSettings["UseKerning"]), fontSettings["Style"]);
return spriteFont;
}
else
{
return null;
}
}
private SoundEffect LoadSoundEffect(string asset)
{
SoundEffect soundEffect = new SoundEffect();
soundEffect.Clip = (UnityEngine.AudioClip)UnityEngine.Resources.Load(asset);
return soundEffect;
}
private Song LoadSong(string asset)
{
Song song = new Song();
song.Clip = (UnityEngine.AudioClip)UnityEngine.Resources.Load(asset);
return song;
}
internal void Unload()
{
// TODO
}
public string RootDirectory { get; set; }
}
}