Skip to content

Commit

Permalink
Add an FPS indicator and an object tree viewer helper class.
Browse files Browse the repository at this point in the history
angavrilov committed Jan 19, 2014
1 parent 7e66841 commit 0af0c6e
Showing 6 changed files with 142 additions and 3 deletions.
3 changes: 2 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -9,7 +9,8 @@ libraries, which are distributed under their own licenses:

Source: https://github.com/angavrilov/mono-tools

Therefore, all original code and scripts are also placed under GNU LGPL.
Therefore, all new code and scripts are also placed under GNU LGPL,
unless noted otherwise in the file itself.


------------------------------------------------------------------------
1 change: 1 addition & 0 deletions Misc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.dll
55 changes: 55 additions & 0 deletions Misc/FPS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// LICENSE: Public Domain

using System;
using UnityEngine;

namespace FPS
{
[KSPAddon(KSPAddon.Startup.EveryScene, false)]
public class FPSIndicator : MonoBehaviour
{
// Compute average FPS over this many frames
private const int NUM_SAMPLES = 10;

private float[] times;
private int cur_sample = 0;

private Rect pos;
private string fps;
private GUIStyle style = null;

FPSIndicator()
{
times = new float[NUM_SAMPLES];

float time = Time.realtimeSinceStartup;
for (int i = 0; i < NUM_SAMPLES; i++)
times[i] = time;
}

public void LateUpdate()
{
float time = Time.realtimeSinceStartup;
float delta = time - times[cur_sample];
times[cur_sample] = time;
cur_sample = (cur_sample+1) % NUM_SAMPLES;

fps = (NUM_SAMPLES / delta).ToString("F1");
}

public void OnGUI()
{
if (style == null)
{
pos = new Rect(Screen.width - 60, 0, 50, 60);

style = new GUIStyle(GUI.skin.label);
style.alignment = TextAnchor.UpperRight;
style.normal.textColor = new Color(0.7f, 0.7f, 0.7f, 0.5f);
}

GUI.Label(pos, fps, style);
}
}
}

54 changes: 54 additions & 0 deletions Misc/UnityObjectTree.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// LICENSE: Public Domain

using System;
using UnityEngine;

// A wrapper class for viewing GameObject hierarchy in the debugger.
// To use, add a watch for "UnityObjectTree.Wrap(whatever)".
class UnityObjectTree
{
public GameObject _self;
public Component[] components;

public override string ToString()
{
return _self.ToString();
}

UnityObjectTree(GameObject obj, UnityObjectTree parent = null) {
this._self = obj;
this.components = obj.GetComponents<Component>();
this.z_parent = parent;
}

public static UnityObjectTree Wrap(GameObject obj) {
return new UnityObjectTree(obj);
}
public static UnityObjectTree Wrap(Component obj) {
return new UnityObjectTree(obj.gameObject);
}

private UnityObjectTree[] z_children;
public UnityObjectTree[] children
{
get {
if (z_children == null && _self.transform)
{
z_children = new UnityObjectTree[_self.transform.childCount];
for (int i = 0; i < _self.transform.childCount; i++)
z_children[i] = new UnityObjectTree(_self.transform.GetChild(i).gameObject, this);
}
return z_children;
}
}

private UnityObjectTree z_parent;
public UnityObjectTree parent
{
get {
if (z_parent == null && _self.transform && _self.transform.parent)
z_parent = new UnityObjectTree(_self.transform.parent.gameObject);
return z_parent;
}
}
}
8 changes: 8 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -36,6 +36,11 @@ On linux the mono package from your distribution should work.
Likewise, for debugging you need MonoDevelop, for example the version that
comes with Unity tools.

The package also includes some small ordinary mods in the ``Misc`` subfolder,
specifically an FPS indicator, and a class to help view the hierarchy of Unity
GameObjects in the debugger watch window. These should be simply placed in
GameData as any other mod.


=========
Profiling
@@ -148,6 +153,9 @@ Setting up the project

Breakpoints don't seem to be recognized if you manually copy the DLL.

As an alternative, on linux it may also work if instead of copying you create
a symbolic link in GameData that points to your dll in the build directory.

3. Right click on everything in *References* and uncheck the *Local copy* option.

This is to stop the build process from copying ``Assembly-CSharp.dll`` and
24 changes: 22 additions & 2 deletions package.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
#!/bin/bash

if [ -z "$KSP_PATH" ]; then
KSP_PATH=~/Games/KSP_linux
fi

if [ ! -d "$KSP_PATH" ]; then
echo "KSP path $KSP_PATH not found."
exit 1
fi

LPATH="$KSP_PATH/KSP_Data/Managed"

compile_ksp() {
LIBNAME=$1
shift
gmcs -r:"$LPATH/Assembly-CSharp.dll,$LPATH/UnityEngine.dll" -t:library "$@" -out:"$LIBNAME"
}

compile_ksp Misc/FPS.dll Misc/FPS.cs
compile_ksp Misc/UnityObjectTree.dll Misc/UnityObjectTree.cs

MONODIR=mono/builds/embedruntimes/

cpstrip() {
@@ -23,6 +43,6 @@ rst2html README.rst > README.html

rm -f devtools-linux32.zip devtools-win32.zip pdb-win32.zip

zip -r devtools-linux32.zip KSP_linux Tools LICENSE README.html -x \*.gitignore Tools/emveepee.bat
zip -r devtools-win32.zip KSP_win Tools LICENSE README.html -x \*.gitignore Tools/emveepee.sh
zip -r devtools-linux32.zip KSP_linux Tools Misc LICENSE README.html -x \*.gitignore Tools/emveepee.bat
zip -r devtools-win32.zip KSP_win Tools Misc LICENSE README.html -x \*.gitignore Tools/emveepee.sh
zip pdb-win32.zip PDB/*.pdb

0 comments on commit 0af0c6e

Please sign in to comment.