Skip to content

Commit

Permalink
update unity3d mode
Browse files Browse the repository at this point in the history
  • Loading branch information
adolli committed Mar 12, 2018
1 parent 833ffda commit 7a25623
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 27 deletions.
82 changes: 60 additions & 22 deletions lib/modules/viewer/backend/unity3d-connector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from 'react'
import autoBind from 'react-autobind'
import _ from 'lodash'
import linkState from 'react-link-state'
import {Tabs, Tab} from 'react-bootstrap'

import IconButton from '../../util/IconButton'
import {TinyLabeledInput, Checkbox} from '../../util/Form'
Expand All @@ -28,6 +29,11 @@ class DevicesTracker extends AdbDevices {
}


const PLATFORM_ANDROID = 1
const PLATFORM_WINDOWS = 2
const PLATFORM_IOS = 3


export class Unity3dDeviceConnector extends React.Component {
constructor(props) {
super(props)
Expand All @@ -37,6 +43,9 @@ export class Unity3dDeviceConnector extends React.Component {
ip: initialState.ip || 'localhost',
port: initialState.port || '5001',
useAdbForward: initialState.useAdbForward || false,
connectUnityWindowsAppUnityEditorMode: initialState.connectUnityWindowsAppUnityEditorMode || false,
windowsTitleRe: initialState.windowsTitleRe || '^.*unity3d game.*$',
platformSelectionKey: initialState.platformSelectionKey || 1,

// adb forward 模式下才有用
devices: {},
Expand All @@ -56,22 +65,30 @@ export class Unity3dDeviceConnector extends React.Component {
return
}

if (this.state.useAdbForward) {
let platform = ''
let options = {}
if (this.state.platformSelectionKey === PLATFORM_ANDROID) {
platform = 'android'
if (this.state.useAdbForward) {
ip = 'localhost'
this.adbClient.forward(this.state.selectedDeviceSerialNo, `tcp:${port}`, `tcp:${port}`)
.then(() => {
this.props.onConnectDevice(view)
})
}
} else if (this.state.platformSelectionKey === PLATFORM_WINDOWS) {
ip = 'localhost'
}

let view = <Unity3dInspectorView ip={ip} port={iport} useAdbForward={this.state.useAdbForward} />

if (this.state.useAdbForward) {
this.adbClient.forward(this.state.selectedDeviceSerialNo, `tcp:${port}`, `tcp:${port}`)
.then(() => {
this.props.onConnectDevice(view)
})
platform = 'windows'
options.titleRe = this.state.windowsTitleRe
options.isUnityEditor = this.state.connectUnityWindowsAppUnityEditorMode
} else {
this.props.onConnectDevice(view)
platform = 'any'
}

localStorage.setItem('Unity3dDeviceConnector.default.initialState', JSON.stringify(this.state))

let view = <Unity3dInspectorView ip={ip} port={iport} platform={platform} options={options} />
this.props.onConnectDevice(view)
}

prepareDeviceTracker() {
Expand All @@ -82,6 +99,9 @@ export class Unity3dDeviceConnector extends React.Component {
this.deviceTracker = null
}
}
handleSelectPlatform(key) {
this.setState({platformSelectionKey: key})
}

componentDidUpdate() {
this.prepareDeviceTracker()
Expand All @@ -108,22 +128,40 @@ export class Unity3dDeviceConnector extends React.Component {

return <div style={{marginTop: '10px'}}>
<div style={{width: '50%'}}>
{!this.state.useAdbForward &&
{!(this.state.useAdbForward || this.state.platformSelectionKey === PLATFORM_WINDOWS) &&
<div><TinyLabeledInput required valueLink={linkState(this, 'ip')} label='IP' /></div>
}
{this.state.useAdbForward &&
{(this.state.useAdbForward || this.state.platformSelectionKey === PLATFORM_WINDOWS) &&
<div><TinyLabeledInput readOnly value='localhost' label='IP' /></div>
}
<div><TinyLabeledInput required valueLink={linkState(this, 'port')} label='Port' /></div>
<div><Checkbox checkedLink={linkState(this, 'useAdbForward')} label='Use adb forward' /></div>

{this.state.useAdbForward && <span>
{devlist.length > 0 && <div>
<div style={{marginTop: '10px', color: '#aaaaaa'}}>Select one of the following devices to forward port.</div>
<div>{devlist}</div>
</div>}
{devlist.length === 0 && <div style={{marginTop: '10px', color: 'orangered'}}>Error: No device available. Connect at lease one Android device to this PC/mac or input the IP address of your mobile device.</div>}
</span>}

<div style={{height: '10px'}}></div>
<Tabs activeKey={this.state.platformSelectionKey} onSelect={this.handleSelectPlatform} id="platform-selection-tab">
<Tab eventKey={1} title="Android" style={{padding: '5px'}}>
<div><Checkbox checkedLink={linkState(this, 'useAdbForward')} label='Use adb forward' /></div>
{this.state.useAdbForward && <div style={{paddingLeft: '20px'}}>
{devlist.length > 0 && <div>
<div style={{marginTop: '10px', color: '#aaaaaa'}}>Select one of the following devices to forward port.</div>
<div>{devlist}</div>
</div>}
{devlist.length === 0 && <div style={{marginTop: '10px', color: 'orangered'}}>Error: No device available. Connect at lease one Android device to this PC/mac or input the IP address of your mobile device.</div>}
</div>}
</Tab>
<Tab eventKey={2} title="Windows" style={{padding: '5px'}}>
<div>
<div><Checkbox checkedLink={linkState(this, 'connectUnityWindowsAppUnityEditorMode')} label='Unity editor mode' /></div>
{!this.state.connectUnityWindowsAppUnityEditorMode && <span>
<div><TinyLabeledInput required valueLink={linkState(this, 'windowsTitleRe')} label='Window title matcher regex' /></div>
</span>}
</div>
</Tab>
<Tab eventKey={3} title='iOS' style={{padding: '5px'}}>
<div className='text-secondary'>No need to configuration</div>
</Tab>
</Tabs>



<div style={{marginTop: '15px'}}><IconButton disabled={connectDisabled} btnClass='btn btn-sm btn-success' text='Connect' onClick={this.handleConnectDevice} /></div>
</div>
Expand Down
21 changes: 16 additions & 5 deletions lib/modules/viewer/backend/unity3d.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,27 @@ export class Unity3dInspectorView extends InspectorViewBase {
this.pocoProc.stdin.write('\n')
}

connectAirtestDevice(devUri) {
let code = `
from airtest.core.api import connect_device
connect_device('${devUri}')
`
this.execPy(code)
}

refresh(width) {
toastr["info"]('Please wait for the screen initializing.')
let isWindowsMode = !this.props.useAdbForward && (this.props.ip === 'localhost' || this.props.ip.startsWith('127.0'))
let isUnityEditor = this.props.platform === 'windows' && this.props.options.isUnityEditor
// 只有windows版的非editor mode 才需要主动connect到device
if (this.props.platform === 'windows' && !this.props.options.isUnityEditor) {
this.connectAirtestDevice(`Windows:///?title_re=${this.props.options.titleRe}`)
}

let code = `
def get_hierarchy_and_screen():
# cache poco instance globally to speed up
poco = globals().get('poco')
if poco is None:
poco = UnityPoco(("${this.props.ip}", ${this.props.port}), ${isWindowsMode ? 'True' : 'False'})
poco = UnityPoco(("${this.props.ip}", ${this.props.port}), ${isUnityEditor ? 'True' : 'False'}, connect_default_device=False)
globals()['poco'] = poco
try:
Expand Down Expand Up @@ -158,13 +169,13 @@ get_hierarchy_and_screen()
this.execPy(code)
}
getSDKVersion() {
let isWindowsMode = !this.props.useAdbForward && (this.props.ip === 'localhost' || this.props.ip.startsWith('127.0'))
let isUnityEditor = this.props.platform === 'windows' && this.props.options.isUnityEditor
let code = `
def get_sdk_version():
# cache poco instance globally to speed up
poco = globals().get('poco')
if poco is None:
poco = UnityPoco(("${this.props.ip}", ${this.props.port}), ${isWindowsMode ? 'True' : 'False'})
poco = UnityPoco(("${this.props.ip}", ${this.props.port}), ${isUnityEditor ? 'True' : 'False'}, connect_default_device=False)
globals()['poco'] = poco
try:
Expand Down
1 change: 1 addition & 0 deletions lib/modules/viewer/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class MainConnector extends React.Component {
{!this.state.inspectorViewInstance && <div>
<div style={{backgroundColor: '#222', padding: '5px 15px', position: 'relative'}}>
<h3>Poco Hierarchy Viewer</h3>
<div style={{position: 'absolute', right: 0, top: 0, padding: '4px', fontSize: '12px'}} className='text-secondary'>version: 1.0.0</div>
<div>
<span>mode: </span>
<DropdownSelectionFixed selections={[MODE_ANDROID_APP, MODE_UNITY]} valueLink={linkState(this, 'connectorMode')} onSelect={this.handleModeChanged} />
Expand Down
6 changes: 6 additions & 0 deletions static/css/layout.css
Original file line number Diff line number Diff line change
Expand Up @@ -910,3 +910,9 @@ h1, h2, h3, h4, h5, h6, p {
font-feature-settings: 'liga';
}

.nav-tabs {
background-color: #444;
}
.nav-tabs > li.active {
background-color: #666;
}

0 comments on commit 7a25623

Please sign in to comment.