Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
Using EditorPrefs to store AboutToStart state. Throw Exception when a…
Browse files Browse the repository at this point in the history
…ttempting to initialize MainThreadDispatcher from worker thread. Demonstrated in WorkerThreadTest scene. Added .meta file for MainThreadDispatcher with negative ExecutionOrder.
  • Loading branch information
movrajr committed Oct 14, 2014
1 parent d68eef1 commit 8707f01
Show file tree
Hide file tree
Showing 8 changed files with 384 additions and 19 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
*/publish
$tf
TestResults
*.meta
!MainThreadScheduler.cs.meta
!*.sln
!*.csproj
!*/*.csproj
*.meta
[Ll]ibrary/
[Tt]emp/
[Oo]bj/
Expand Down Expand Up @@ -42,7 +43,7 @@ x64/
*_i.c
*_p.c
*.ilk
*.meta
# *.meta # already ignored in Unity section
*.obj
*.pch
*.pdb
Expand Down
9 changes: 6 additions & 3 deletions Assets/ObjectTest/EditorBehaviourTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -24,7 +24,6 @@ void Start()
// .Delay(TimeSpan.FromSeconds(3))
// .Subscribe(x => Debug.Log(x));


//Observable.Interval(TimeSpan.FromSeconds(1))
// .Take(10)
// .Subscribe(x => Debug.Log(x));
Expand All @@ -45,6 +44,8 @@ void Update()
}
}

#if UNITY_EDITOR

public static class MyEditor
{
[UnityEditor.MenuItem("UniRxTest/Fire", false)]
Expand All @@ -64,7 +65,7 @@ public static void Thread()
[UnityEditor.MenuItem("UniRxTest/ScenePlaybackDectector.IsPlaying", false)]
public static void IsPlaying()
{
// huga huga hug 2
// huga huga hug 2
Debug.Log(UniRx.ScenePlaybackDetector.IsPlaying);
}

Expand All @@ -76,4 +77,6 @@ static IEnumerator Hoge()
Debug.Log(www.text);
}
}

#endif
}
27 changes: 27 additions & 0 deletions Assets/ObjectTest/WorkerThreadTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Threading;
using UniRx;
using UnityEngine;

public class WorkerThreadTest : MonoBehaviour
{
void Awake()
{
Thread worker = new Thread(() =>
{
// Bind to application quit event.
MainThreadDispatcher.OnApplicationQuitAsObservable().Subscribe(_ => Debug.Log("OnApplicationQuitAsObservable"));
// Create capsules one by one.
Observable.Interval(TimeSpan.FromMilliseconds(300))
.Take(5)
.Subscribe((s) =>
{
var g = GameObject.CreatePrimitive(PrimitiveType.Capsule);
g.name = "Capsule " + s;
g.transform.position += new Vector3(s, 0, 0);
});
});
worker.Start();
}
}
262 changes: 262 additions & 0 deletions Assets/Scenes/WorkerThreadTest.unity
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!29 &1
SceneSettings:
m_ObjectHideFlags: 0
m_PVSData:
m_PVSObjectsArray: []
m_PVSPortalsArray: []
m_OcclusionBakeSettings:
smallestOccluder: 5
smallestHole: .25
backfaceThreshold: 100
--- !u!104 &2
RenderSettings:
m_Fog: 0
m_FogColor: {r: .5, g: .5, b: .5, a: 1}
m_FogMode: 3
m_FogDensity: .00999999978
m_LinearFogStart: 0
m_LinearFogEnd: 300
m_AmbientLight: {r: .200000003, g: .200000003, b: .200000003, a: 1}
m_SkyboxMaterial: {fileID: 0}
m_HaloStrength: .5
m_FlareStrength: 1
m_FlareFadeSpeed: 3
m_HaloTexture: {fileID: 0}
m_SpotCookie: {fileID: 0}
m_ObjectHideFlags: 0
--- !u!127 &3
LevelGameManager:
m_ObjectHideFlags: 0
--- !u!157 &4
LightmapSettings:
m_ObjectHideFlags: 0
m_LightProbes: {fileID: 0}
m_Lightmaps: []
m_LightmapsMode: 1
m_BakedColorSpace: 0
m_UseDualLightmapsInForward: 0
m_LightmapEditorSettings:
m_Resolution: 50
m_LastUsedResolution: 0
m_TextureWidth: 1024
m_TextureHeight: 1024
m_BounceBoost: 1
m_BounceIntensity: 1
m_SkyLightColor: {r: .860000014, g: .930000007, b: 1, a: 1}
m_SkyLightIntensity: 0
m_Quality: 0
m_Bounces: 1
m_FinalGatherRays: 1000
m_FinalGatherContrastThreshold: .0500000007
m_FinalGatherGradientThreshold: 0
m_FinalGatherInterpolationPoints: 15
m_AOAmount: 0
m_AOMaxDistance: .100000001
m_AOContrast: 1
m_LODSurfaceMappingDistance: 1
m_Padding: 0
m_TextureCompression: 0
m_LockAtlas: 0
--- !u!196 &5
NavMeshSettings:
m_ObjectHideFlags: 0
m_BuildSettings:
agentRadius: .5
agentHeight: 2
agentSlope: 45
agentClimb: .400000006
ledgeDropHeight: 0
maxJumpAcrossDistance: 0
accuratePlacement: 0
minRegionArea: 2
widthInaccuracy: 16.666666
heightInaccuracy: 10
m_NavMesh: {fileID: 0}
--- !u!1 &90993553
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 4
m_Component:
- 4: {fileID: 90993555}
- 132: {fileID: 90993554}
m_Layer: 0
m_Name: Explanation
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!132 &90993554
GUIText:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 90993553}
m_Enabled: 1
serializedVersion: 3
m_Text: "If you run this scene without modification, an exception will be thrown.\n\nThat's
because in WorkerThreadTest.Awake() a new worker thread is\nstarted that calls
UniRx for the first time in the game. \n\nUniRx depends on MainThreadDispatcher,
which is a MonoBehaviour\nand can therefore only be created on the main thread.\n\nNormally
UniRx runs on the main thread and will try to create a\nGameObject with a MainThreadDispatcher
component if it doesn't\nfind one.\n\nBut if the first time UniRx is called is
in a worker thread, you need to\nmake sure MainThreadDispatcher is already present
in the scene.\n\nIn the situation of this scene, where UniRx is called in a worker
thread\nin Awake(), MainThreadDispatcher's Execution Order needs a\nnegative value
to initialize it before everything else.\n\nTo make this scene run:\n\n1) Go to
Edit > Project Settings > Script Execution Order.\n Add UniRx.MainThreadDispatcher
and make sure that \n it has a negative value.\n\n2) Add a MainThreadDispatcher
component to the scene.\n\n3) Play the scene!\n\nSee also: https://github.com/neuecc/UniRx/issues/33"
m_Anchor: 0
m_Alignment: 0
m_PixelOffset: {x: 0, y: 0}
m_LineSpacing: 1
m_TabSize: 4
m_Font: {fileID: 0}
m_Material: {fileID: 0}
m_FontSize: 0
m_FontStyle: 0
m_Color:
serializedVersion: 2
rgba: 4294967295
m_PixelCorrect: 1
m_RichText: 1
--- !u!4 &90993555
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 90993553}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: .300000012, y: .949999988, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 2
--- !u!1 &592012134
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 4
m_Component:
- 4: {fileID: 592012140}
- 20: {fileID: 592012139}
- 92: {fileID: 592012138}
- 124: {fileID: 592012137}
- 81: {fileID: 592012136}
m_Layer: 0
m_Name: Main Camera
m_TagString: MainCamera
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!81 &592012136
AudioListener:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 592012134}
m_Enabled: 1
--- !u!124 &592012137
Behaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 592012134}
m_Enabled: 1
--- !u!92 &592012138
Behaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 592012134}
m_Enabled: 1
--- !u!20 &592012139
Camera:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 592012134}
m_Enabled: 1
serializedVersion: 2
m_ClearFlags: 1
m_BackGroundColor: {r: .210586071, g: .256341726, b: .301470578, a: .0196078438}
m_NormalizedViewPortRect:
serializedVersion: 2
x: 0
y: 0
width: 1
height: 1
near clip plane: .300000012
far clip plane: 1000
field of view: 60
orthographic: 1
orthographic size: 5
m_Depth: -1
m_CullingMask:
serializedVersion: 2
m_Bits: 4294967295
m_RenderingPath: -1
m_TargetTexture: {fileID: 0}
m_TargetDisplay: 0
m_HDR: 0
m_OcclusionCulling: 1
m_StereoConvergence: 10
m_StereoSeparation: .0219999999
--- !u!4 &592012140
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 592012134}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: -10}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
--- !u!1 &1951413495
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 4
m_Component:
- 4: {fileID: 1951413497}
- 114: {fileID: 1951413496}
m_Layer: 0
m_Name: WorkerThreadTest
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1951413496
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1951413495}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: dca0dc71a1038104391fbfe361f092e7, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!4 &1951413497
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1951413495}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1

0 comments on commit 8707f01

Please sign in to comment.